C# 委托、Action委托、Func委托、多播委托、匿名方法、Lambda表达式

    class Program
    {
        delegate void delegate_write(string str);
        delegate int delegate_Sum(int a,int b);
        delegate int delegate_Test(int a, int b);

        delegate string delegate_GetString();

        static void Main(string[] args)
        {
            delegate_write delewrite = new delegate_write(write);
            delewrite("123");

            delegate_Sum deleSum = Sum;//可以通过直接赋值的方法创建委托           
            Console.WriteLine(deleSum(1, 2));

            int x = 10;
            delegate_GetString GetString = x.ToString;//委托也可以直接指向实例方法
            Console.WriteLine(GetString());

            delegate_Test[] delegate_test = { Sum, Multiply };
            foreach (delegate_Test dt in delegate_test)
            {
                //Console.WriteLine(dt(3,3));//委托直接调用
                ProcessAndDisplayRes(dt,3,3);//将委托向参数一样传递,与上一行运行结果相同
            }
            Console.Read();
        }

        static void write(string str)
        {
            Console.WriteLine(str);
        }
        static int Sum(int a,int b)
        {
            return a + b;
        }

        static int Multiply(int a, int b)
        {
            return a * b;
        }

        static void ProcessAndDisplayRes(delegate_Test dt,int a,int b)
        {     
            Console.WriteLine(dt(a, b));
        }

    }

运行结果:

123
3
10
6
9

Action委托:

 Action只能指向没有返回值的方法,但参数可以是任意多个

        static void Main(string[] args)
        {
       		//Action只能指向没有返回值的方法,但参数可以是任意多个
            Action Method = Test1;
            Method();

            Action<int> Method1 = Test2;
            Method1(10);

            Action<int ,double> Method2 = Test3;
            Method2(10,10.5);

            Console.Read();
        }
        public static void Test1()
        {
            Console.WriteLine("Test1 ");
        }
        public static void Test2(int x)
        {
            Console.WriteLine("Test2 " + x);
        }
        public static void Test3(int x,double y)
        {
            Console.WriteLine("Test3 " + x +y);
        }

运行结果:

Test1
Test2 10
Test3 1010.5

Func委托:

 Func必须指向有返回值的方法

    class Program
    {

        static void Main(string[] args)
        {
            //Func必须指向有返回值的方法
            Func<string> Methon1 = Test1;//当只有返回值时,泛型只指定返回值类型
            Console.WriteLine(Methon1());

            Func<int,string> Methon2 = Test2;//既有参数又有返回值的话,泛型先指定参数类型,再指定返回值类型
            Console.WriteLine(Methon2(10));

            Func<int, double,double> Methon3 = Test3;//既有参数又有返回值的话,泛型先指定参数类型,再指定返回值类型
            Console.WriteLine(Methon3(10,1.2));

            Console.Read();
        }

        public static string Test1()
        {
            return "Test1 ";
        }
        public static string Test2(int x)
        {
            return "Test2 " + x;
        }
        public static double Test3(int x, double y)
        {
            return x + y;
        }
    }

运行结果:

Test1
Test2 10
11.2

多播委托:

包含多个方法的委托成为多播委托,调用多播委托,可以按照顺序连续调用多个方法,多播委托只能得到委托调用的最好一个方法的结果,因此,我们常把多播委托的返回值定义为void;

  多播委托可以用运算符"+“和”+=“给委托添加方法调用,同样也可以用运算符”-“和”-="给委托删除方法调用,
        调用时,按照方法被添加的顺序依次执行。注意,对于委托,+= 和 -= 对null是不会报错的
        多播委托包含一个逐个调用委托集合,如果通过委托嗲用的其中一个方法抛出一个异常,整个迭代就会停止;为了避免这个问题,应该自己迭代方法列表。Delegate类定义GetInvocationList()方法,他返回一个Delegate对象数组

    class Program
    {
        static void Main(string[] args)
        {
            Action Method = Test1;
            Method += Test2;
            Method += Test3;
            Method();
            Console.WriteLine("..........");

            Method -= Test1;
            Method -= Test2;
            Method();
            Console.WriteLine("..........");

            Method -= null;//对null操作不会报错
            Method += null;
            Method += Test1;
            Method += Test2;
            Delegate[] delegates = Method.GetInvocationList();
            foreach(Delegate d in delegates)
            {
                d.DynamicInvoke();
            }
            Console.Read();
        }
        public static void Test1()
        {
            Console.WriteLine("Test1 ");
        }
        public static void Test2()
        {
            Console.WriteLine("Test2 ");
        }
        public static void Test3()
        {
            Console.WriteLine("Test3 ");
        }
    }

运行结果

Test1
Test2
Test3
..........
Test3
..........
Test3
Test1
Test2

匿名方法:

  常规使用委托,都是先定义一个方法,然后把方法赋值给委托实例;但还有另外一种使用委托的方法,不用去定义方法,而是使用匿名方法。相当于直接把要引用的方法写在了后面,优点是减少了要编写的代码,减小代码的复杂性。

        static void Main(string[] args)
        {
            Func<int, int, int> Sum = delegate (int a, int b) 
            {
                return a + b;
            };
            int c = Sum(1,2);
            Console.WriteLine(c);
            Console.Read();
        }

运行结果:

3

Lambda表达式:

“Lambda表达式"是一个匿名函数,是一种高效的类似于函数式编程的表达式,Lambda简化了开发中需要编写的代码量。它可以包含表达式和语句,并且可用于创建委托或表达式目录树类型,支持带有可绑定到委托或表达式树的输入参数的内联表达式。所有Lambda表达式都使用Lambda运算符=>,该运算符读作"goes to”。Lambda运算符的左边是输入参数(如果有),右边是表达式或语句块。
语法说明:
 参数类型可省略
 一行执行内容时大括号和Return可省略,方法有多行的时候一定要加上{}与return
 当只有一个参数的时候,传递参数的()可以省略
 Lambda表达式可以访问Lambda表达式代码块外的变量,需谨慎使用

        static void Main(string[] args)
        {
            //Func<int, int, int> Sum = (int a, int b) =>{ return a + b; };
            //参数类型可省略
            //Func<int, int, int> Sum = ( a,  b) => { return a + b; };
            //一行执行内容时大括号和Return可省略
            Func<int, int, int> Sum = ( a,  b) => a + b;
            int c = Sum(2, 3);
            Console.WriteLine(c);
            /*注意:方法有多行的时候一定要加上{}与return*/

            //Func<double, double> square = (x) => x * x;
            //当只有一个参数的时候,传递参数的()可以省略
            Func<double, double> square = x => x * x;
            Console.WriteLine(square(1.1));
            
            //Lambda表达式可以访问Lambda表达式代码块外的变量
            int e = 5;
            Func<int, int> fun = f => e + f;
            Console.WriteLine(fun(4));
            e = 8;
            Console.WriteLine(fun(4));

            Console.Read();
        }

运行结果:

5
1.21
9
12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值