C# 中的委托

一:使用委托模仿 Console.ReadLine()的功能

 1 class Program
 2     {
 3         delegate string DelegateReadLine(); //定义委托
 4 
 5         static void Main(string[] args)
 6         {
 7             //声明一个委托,将方法 Console.ReadLine方法名传给它
 8             DelegateReadLine dr = new DelegateReadLine(Console.ReadLine);
 9 
10             string input = dr(); //调用委托,这个委托处理的功能就是 Console.ReadLine()函数处理的功能
11             Console.WriteLine(input);
12         }

二:

 1  class Program
 2     {
 3         //在 函数外,类中定义 委托
 4         delegate double ProcessDelegate(double p1, double p2);  
 5 
 6          //注意  委托的返回类型,参数类型  都和函数 的返回类型和参数类型必须一样
 7         static double Multiply(double p1, double p2)
 8         {
 9             return p1 * p2;
10         }
11         static double Divide(double p1, double p2)
12         {
13             return p1 / p2;
14         }
15 
16         static int Add(int p1, int p2)
17         {
18             return p1 + p2;
19         }
20         static void Main(string[] args)
21         {
22             //声明委托:
23             ProcessDelegate pd;
24 
25             Console.Write("请输入两个数(使用逗号隔开):");
26             string input = Console.ReadLine();
27             string[] str =  input.Split(',');
28             double t1 = Convert.ToDouble(str[0]);
29             double t2 = Convert.ToDouble(str[1]);   //分别获取两个输入的数值
30 
31 
32 
33             pd = new ProcessDelegate(Multiply);  //使用委托来处理 Multiply函数的作用
34             double a1 = pd(t1, t2);  //使用该委托调用所使用的函数
35             Console.WriteLine("x*y={0}", a1);
36 
37             pd = new ProcessDelegate(Divide); //委托的参数是 要使用的函数名
38             double a2 = pd(t1, t2);
39             Console.WriteLine("x/y={0}", a2);
40 
41             pd = Multiply;  //也可以直接调用方法
42             double a3 = pd(t1, t2);
43             Console.Write("x*y={0}",a3);  //也可以使用这种方法
44         }

结果:

三:

 1 namespace DelegateOldBook
 2 {
 3     class Program
 4     {
 5         delegate int  DelegateTest(int x, int y);  //用于调用静态方法
 6         delegate int DelefateTest2(int x, int y);  //用于调用实例方法
 7 
 8         static void Main(string[] args)
 9         {
10 
11 
12             int x = 5, y = 9;
13             DelegateTest dt = new DelegateTest(Add);  //委托对象引用静态方法 Add
14             Console.WriteLine("静态方法:x+y={0}", dt(x, y));   //调用委托变量  dt(x,y)
15             dt = new DelegateTest(Sub);
16             Console.WriteLine("静态方法:x-y={0}", dt(x, y));
17 
18             test t = new test();  //先实例化 test 类
19             DelefateTest2 dt2 = new DelefateTest2(t.Multiply);   //委托引用实例方法 Multiply
20             Console.WriteLine("实例方法:x*y={0}", dt2(5, 5));
21 
22         }
23         static int Add(int x, int y)
24         {
25             return x + y;
26         }
27         static int Sub(int x, int y)
28         {
29             return x - y;
30         }   
31     }
32 
33     class test
34     {
35         public int Multiply(int x, int y)
36         {
37             return x * y;
38         }
39     }
40 }

结果:

原文地址:https://www.cnblogs.com/TangPro/p/3258528.html