5f05997fdc2189267fe3b9392f3e2fc.jpg

委托的使用

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. MyDele dele1 = new MyDele(M1);
  6. Student stu = new Student();
  7. dele1 += stu.SayHello;
  8. dele1.Invoke();
  9. }
  10. static void M1() {
  11. Console.WriteLine("M1 is called");
  12. }
  13. }
  14. class Student
  15. {
  16. public void SayHello()
  17. {
  18. Console.WriteLine("Hello,I'm a student!");
  19. }
  20. }
  21. delegate void MyDele();
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. MyDele dele1 = new MyDele(Add);
  6. int res = dele1(100, 200);
  7. Console.WriteLine(res);
  8. }
  9. static int Add(int x,int y) {
  10. return x + y;
  11. }
  12. }
  13. delegate int MyDele(int a,int b);

泛型委托

  1. MyDele<int> deleAdd = new MyDele<int>(Add);
  2. int res = deleAdd(100, 200);
  3. Console.WriteLine(res);
  4. MyDele<double> deleMul = new MyDele<double>(Mul);
  5. double mulRes = deleMul(3.0, 5.0);
  6. Console.WriteLine(mulRes);
  7. Console.WriteLine(deleAdd.GetType().IsClass);
  8. }
  9. static int Add(int x,int y)
  10. {
  11. return x + y;
  12. }
  13. static double Mul(double x,double y)
  14. {
  15. return x * y;
  16. }
  17. }
  18. delegate T MyDele<T>(T a, T b);

d544c2eb589f9f1fd209ddc2f22ed9d.png

运用类库直接委托不用声明

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Action action = new Action(M1);
  6. action();
  7. Action<string> action2= new Action<string>(SayHello);
  8. action2("Tim");
  9. Func<int, int, int> func = new Func<int, int, int>(Add);
  10. int res = func(100, 200);
  11. Console.WriteLine(res);
  12. }
  13. static void M1()
  14. {
  15. Console.WriteLine("M1 is called!");
  16. }
  17. static void SayHello(string name)
  18. {
  19. Console.WriteLine("Hello,{0}",name);
  20. }
  21. static int Add(int x,int y)
  22. {
  23. return x + y;
  24. }
  25. static double Mul(double x,double y)
  26. {
  27. return x * y;
  28. }
  29. }

有返回值用Func<>,无返回值用Action<>。

Lambda表达式

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Func<int, int, int> func = new Func<int, int, int>((int a, int b) => { return a + b; });
  6. int res = func(100, 200);
  7. Console.WriteLine(res);
  8. func = new Func<int, int, int>((int x, int y) => { return x * y});
  9. res = func(3, 4);
  10. Console.WriteLine(res);
  11. }
  12. }
  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. Func<int, int, int> func = ((int a, int b) => { return a + b; });
  6. int res = func(100, 200);
  7. Console.WriteLine(res);
  8. func =((int x, int y) => { return x * y; });
  9. res = func(3, 4);
  10. Console.WriteLine(res);
  11. }
  12. }

泛型方法+泛型委托类参数+

  1. class Program
  2. {
  3. static void Main(string[] args)
  4. {
  5. // DoSomeCalc<int>((int a, int b)=>{ return a * b; }, 100, 200);
  6. DoSomeCalc(( a, b) => { return a * b; }, 100, 200);//100,200可以推断出来是整型,所以int可以省略
  7. }
  8. static void DoSomeCalc<T>(Func<T, T, T> func, T x, T y)
  9. {
  10. T res=func(x, y);
  11. Console.WriteLine(res);
  12. }
  13. }