action func用法记记_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > action func用法记记

action func用法记记

 2013/9/8 12:36:19  KimhillZhang  博客园  我要评论(0)
  • 摘要:publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}publicdelegatevoidshowMessage(stringmsg);publicstaticvoidshowM(stringm){MessageBox.Show(m);}publicstringreturnM(stringm){returnm;}privatevoidbutton1_Click(objectsender,EventArgse)
  • 标签:用法

public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        public delegate void showMessage(string msg);

        public static void showM(string m)

        {

            MessageBox.Show(m);

        }

        public string returnM(string m)

        {

            return m;

        }

        private void button1_Click(object sender, EventArgs e)

        {

            //3

            showMessage s = new showMessage(showM);

            s("xxx");

            //1

            Action<string> action = showM;

            action("123");

            //2

            Action<string> a = X => showM(X);

            a("aaaaaaaaaa");

            //6

            Action<string> ac = (x1) =>

            {

                MessageBox.Show(x1);

            };

            ac("111111111111111");

            //6

            Action<string> ac2 = delegate(string xx){

                MessageBox.Show(xx);

            };

            ac2("222222222222");

           

            //5

            string m1 = "123";

            textBox1.Invoke((Action<string>)delegate(string m)

            {

                textBox1.Text = m;

            },m1);

            textBox1.Invoke(new Action<string>((x4) =>

            {

                textBox1.Text = x4;

            }), "aaaaax4");

 

            //4 --需要return

            Func<string, string> f = returnM;

            MessageBox.Show(f("abc"));

            //7

            Func<string> func = delegate()

            {

                return "我是Func<TResult>委托出来的结果";

            };

            MessageBox.Show(func());

            //8

            Func<string, string> funcOne = delegate(string s3)

            {

                return s3;

            };

            MessageBox.Show(funcOne("我是Func<T,TResult>委托出来的结果"));

 

            //9

            Func<string, string, string> funcTwo = delegate(string value1, string value2)

            {

                return value1 + " " + value2;

            };

            MessageBox.Show(funcTwo("xxx", "我是Func<T,TResult>委托出来的结果"));

            //10

            Func<string, string, string> ff = (x1, x2) =>

            {

                return x1 + " " + x2;

            };

            MessageBox.Show(funcTwo("xxx", "我是Func<T,TResult>委托出来的结果"));

        }

    }

发表评论
用户名: 匿名