码迷,mamicode.com
首页 > Windows程序 > 详细

04.C#委托、Lambda表达式

时间:2015-03-05 14:37:29      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

1.委托

(1)委托的本质
    委托实际上就是指向函数的指针。在C#中委托是一种类型,定义一个委托就是定义一个新的类,它与类的地位是一样的,所以可以定义类的地方都可以定义委托!
    实际上,使用delegate关键字定义的委托继承了System.MulticastDelegate类,而System.MulticastDelegate类又继承了System.Delegate。
    Delegate:表示委托,委托是一种数据结构,它引用静态方法或引用类实例及该类的实例方法。
    MulticastDelegate:表示多路广播委托;即,其调用列表中可以拥有多个元素的委托。
(2)使用委托的示例
  1. delegate string DelegateMethod(int a, int b);//定义委托
  2. class Program
  3. {
  4. static void Main(string[] args)
  5. {
  6. DelegateMethod dm = new DelegateMethod(Method);//或:DelegateMethod dm = Method
  7. Console.WriteLine(dm(10, 20));//执行委托
  8. Console.Read();
  9. }
  10. private static string Method(int a, int b)
  11. {
  12. return "相加结果:" + (a + b);
  13. }
  14. }
(3)委托的Invoke与BeginInvoke方法
1、Invoke方法
    实际上,给委托实例提供圆括号与调用委托的Invoke方法效果完全相同:
  1. delegate string DelegateMethod(int a, int b);//定义委托
  2. class Program
  3. {
  4. static void Main(string[] args)
  5. {
  6. DelegateMethod dm = Method;
  7. Console.WriteLine(dm.Invoke(10, 20));//执行委托
  8. Console.Read();
  9. }
  10. private static string Method(int a, int b)
  11. {
  12. return "相加结果:" + (a + b);
  13. }
  14. }
2、BeginInvoke方法
    调用委托实例的BeginInvoke方法就是开启一个新的线程执行委托实例指向的方法。
  1. delegate void DelegateMethod(int a, int b);//定义委托
  2. class Program
  3. {
  4. static void Main(string[] args)
  5. {
  6. DelegateMethod dm = Method;
  7. for (int i = 0; i < 10; i++)
  8. {
  9. dm.BeginInvoke(i, 20, null, null);//异步调用委托
  10. }
  11. Console.Read();
  12. }
  13. private static void Method(int a, int b)
  14. {
  15. Console.WriteLine("相加结果:" + (a + b));//执行委托
  16. }
  17. }
    BeginInvoke方法有三种参数,第一种参数是委托实例指向的方法的参数(可能有多个);第二种参数的类型是AsyncCallback,AsyncCallback是一个委托类型,其定义是delegate void AsyncCallback(IAsyncResult ar),当调用BeginInvoke方法的委托实例异步执行完成时,就会执行该参数指向的方法;第三种参数是object类型,主要是向第二种参数传递一些值,一般可以传递被调用方法的委托,这个值可以使用IAsyncResult.AsyncState属性获得。(具体请参考“C#多线程”)
  1. delegate void DelegateMethod(int a, int b);//定义委托
  2. class Program
  3. {
  4. static void Main(string[] args)
  5. {
  6. DelegateMethod dm = Method;
  7. dm.BeginInvoke(10, 20, MethodCompleted, null);//异步调用委托
  8. Console.Read();
  9. }
  10. //异步委托
  11. private static void Method(int a, int b)
  12. {
  13. Console.WriteLine("相加结果:" + (a + b));
  14. Thread.Sleep(3000);
  15. }
  16. //回调函数
  17. private static void MethodCompleted(IAsyncResult ar)
  18. {
  19. Console.WriteLine("休眠结束!");
  20. }
  21. }
3、委托的EndInvoke方法
    如果调用了委托实例的BeginInvoke方法,就可以通过EndInvoke方法获得委托实例指向的方法的返回值,或是确定指向的方法已经被成功调用。(具体请参考“C#多线程”)
  1. delegate string DelegateMethod(int a, int b);//定义委托
  2. class Program
  3. {
  4. static void Main(string[] args)
  5. {
  6. DelegateMethod dm = Method;
  7. IAsyncResult ar = dm.BeginInvoke(10, 20, null, null);//异步调用委托
  8. string result = dm.EndInvoke(ar);//等待委托异步调用结束
  9. Console.WriteLine(result);//输出返回值
  10. Console.Read();
  11. }
  12. //异步委托
  13. private static string Method(int a, int b)
  14. {
  15. Thread.Sleep(3000);
  16. return "相加结果:" + (a + b);
  17. }
  18. }
(4)Action<T>、Func<T>与Predicate<T>(泛型委托)
    除了为每个参数和返回类型定义一个新委托之外,还可以使用Action<T>与Func<T>泛型委托。使用泛型委托主要的优势是可以省略委托的定义。
1、Action<T>无返回值的泛型委托
    泛型Action<T>委托表示引用一个void返回类型的方法,这个委托类可以有多个(≥0)参数且参数的类型可以不同(最多可以有16种不同类型的参数)。
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Action<int,int> dm = Method;//委托实例
  6. dm(10,20);//调用委托
  7. Console.Read();
  8. }
  9. private static void Method(int a, int b)
  10. {
  11. Console.WriteLine("相加结果:" + (a + b));
  12. }
  13. }
2、Func<T>带有返回值的泛型委托
    泛型Func<T>委托与Action<T>委托类似,不同点是Func<T>允许调用带返回值类型的方法,而且其返回值类型的指定是Func<T1,T2,T3...Tn>中的最后一个类型(Tn),即Tn就是其返回值,其它类型都表示参数的类型。(最多可以有16种参数类型和一个返回值类型)
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Func<int, int, string> dm = Method;//委托实例
  6. string result = dm(10, 20);//调用委托
  7. Console.WriteLine(result);//输出返回值
  8. Console.Read();
  9. }
  10. private static string Method(int a, int b)
  11. {
  12. return "相加结果:" + (a + b);
  13. }
  14. }
3、Predicate<T>委托(常用于集合参数)
    Predicate<T>泛型委托,只能接受一个传入参数,返回值为bool类型。
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Predicate<int> dm = Method;//委托实例
  6. if (dm(12))//调用委托
  7. {
  8. Console.WriteLine("偶数");
  9. }
  10. else
  11. {
  12. Console.WriteLine("奇数");
  13. }
  14. Console.Read();
  15. }
  16. private static bool Method(int a)
  17. {
  18. if(a%2==0)
  19. {
  20. return true;
  21. }
  22. return false;
  23. }
  24. }
(5)多播委托
    前面的委托只包含一个方法,但是委托也可以包含多个方法,这种委托称为多播委托。如果调用多播委托就可以按顺序连续调用多个方法,为此委托的定义就必须返回void,否则就只能得到委托调用的最后一个方法的结果。多播委托可以识别“+、-、+=、-=”运算符。
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Action<int> dm = null; //委托实例
  6. for (int i = 0; i < 20; i++)
  7. {
  8. if (i % 2 == 0)
  9. {
  10. dm += Method1;
  11. }
  12. else
  13. {
  14. dm += Method2;
  15. }
  16. }
  17. dm(123456);//调用多播委托
  18. Console.Read();
  19. }
  20. private static void Method1(int a)
  21. {
  22. Console.WriteLine("委托1:" + a);
  23. }
  24. private static void Method2(int a)
  25. {
  26. Console.WriteLine("委托2:" + a);
  27. }
  28. }
(6)使用委托定义匿名方法
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Action<int> dm = delegate(int a)
  6. {
  7. Console.WriteLine(a.ToString());
  8. };//匿名方法
  9. dm(123456);//调用委托指向的匿名方法
  10. Console.Read();
  11. }
  12. }

2.Lambda表达式

(1)Lambda表达式本质
    Lambda表达式的本质是一个匿名函数,Lambda表达式只能与委托配合使用,其优势在可以很方便的定义匿名方法。所有Lambda表达式都使用Lambda运算符=>,该运算符读作"goes to"。Lambda运算符的左边是输入参数(如果有),右边是表达式或语句块。
(2)Lambda表达式使用示例
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. //标准的Lambda格式
  6. Func<int, int, string> sum0 = (int a, int b) => { return "和是:" + (a + b); };
  7. Console.WriteLine(sum0(10, 20));
  8. //简写格式
  9. Func<int, int, string> sum1 = (a, b) => "和是:" + (a + b);
  10. Console.WriteLine(sum1(30, 40));
  11. Action<int, int> sum2 = (a, b) => Console.WriteLine("和是:" + (a + b));
  12. sum2(50, 60);
  13. Console.Read();
  14. }
  15. }
(3)Lambda表达式语法
1、表达式Lambda语法(参数语法)
    如果只有一个输入参数时,括号可以省略,如:(x) => x * x 等于 x => x * x
    如果具有一个以上的输入参数,必需加上括号,如:(x, y) => x == y
    可以显式指定输入参数的类型,如:(int x, string s) => s.Length > x
    也可以没有任何输入参数,如:() => Console.WriteLine("无参数的Lambda表达式")
2、语句Lambda语法(函数体语法)
    语句写在大括号中,如:
  1. Action<int, int> sum = (a, b) => { int i = a = +b; Console.Write(i); };
    只有一条语句时,可以省略大括号“{}”,如:
  1. Action<int, int> sum = (a, b) => Console.Write(a + b);
    当匿名方法有返回值时,可以使用return,如:
  1. Func<int, int, string> sum0 = (a, b) => { return "和是:" + (a + b); };
    当匿名方法有返回值,且只有一条语句时,可以省略大括号“{}”和return,如:
  1. Func<int, int, string> sum1 = (a, b) => "和是:" + (a + b);
    注意:当有返回值时,语句Lambda中使用了大括号就一定要使用return关键字返回;反之使用了return关键字返回结果就一定要使用大括号。即:return关键字与大括号必须同时省略!
3、类型猜测
    当编写一个Lambda式的时候,我们通常不需要明确指定输入参数的类型。因为编译器会根据Lambda体的实现,以及委托的定义来猜测类型。如:如果要从一个List<int>中删除小于100的元素
  1. List<int> list = new List<int>();
  2. list.RemoveAll(i => i < 100);//i会被猜测为int
    通常的猜测规则如下:
  1. Lambda式必须包含与委托定义中相等数量的输入参数;
  2. 每个Lambda式的输入参数必须能够隐式转换成委托定义中所要求的输入参数;
  3. Lambda式的返回值必须能够隐式转换成委托定义中的返回值。
4、Lambda式中的变量作用域
    在Lambda式定义中可以引用外部变量。只要是在定义处能够访问到的变量,都可以在Lambda式中引用。所有会被引用的外部变量必须在Lambda式定义之前被显式赋值。
    变量作用域的规则:
  1. 被“捕获”的变量在委托的生命周期结束前都不会被垃圾回收;
  2. 在Lambda式内部定义的变量对外不可见;
  3. Lambda式无法直接捕获一个具有ref或out描述的参数变量;
  4. Lambda式中的return语句不会导致当前所在的方法返回;
  5. Lambda式中不允许包含会导致跳当前执行范围的goto,break 或 continue语句。
-------------------------------------------------------------------------------------------------------------------------------




04.C#委托、Lambda表达式

标签:

原文地址:http://www.cnblogs.com/LiZhiW/p/4315643.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!