c# 委托 delegate

本文详细介绍了 C# 中的委托概念,包括其基本用途、如何创建和使用委托、多播委托的功能以及如何实现回调操作,特别是异步回调的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    /// <summary>
    /// 委托:delegates;
    /// 1,允许你引用函数而不是直接调用,通常用于实现称为回调的技术,对特定函数执行调用并通知
    ///   已执行完成。一般用于事件说非常有用。
    /// 2,委托是包含方法引用的引用类型,可认为是指向函数的指针,而非直接调用函数。
    ///   可帮助改进应用程序的响应灵敏性
    /// </summary>

 

 1         /// <summary>
 2         /// 如下是一个委托类型:MethodeDe,无函数主体
 3         /// 创建委托类型:MethodeDe的对象:method,并指向函数Add,
 4         /// 注意:委托类型 和 它指向的函数对象,必须有相同的签名(返回类型和形参)
 5         /// 函数修饰必须为static
 6         /// </summary>
 7         /// <param name="n1"></param>
 8         /// <param name="n2"></param>
 9         /// <returns></returns>
10         private delegate int MethodDe(int n1, int n2);
11 
12         static int Add(int n1, int n2)
13         {
14             return n1 + n2;
15         }
16 
17             //委托的初体验
18             int n1 = 1, n2 = 2;
19             MethodDe method=new MethodDe(Add);  //或:MethodeDe method=Add;
20             MethodDe method2;                   //now the method2 is null
21             method(n1,n2);                      //调用委托类型对象,直接指向 Add            

 

 1         /// <summary>
 2         ///委托链接,即多播委托,使一个委托指向多个函数 
 3         /// </summary>
 4         delegate void MethodDe2();
 5 
 6         static void method1()
 7         {
 8             Console.WriteLine("method1");
 9         }
10         static private void method2()
11         {
12             Console.WriteLine("method2");
13         }
14 
15             //多播委托:
16             //执行的时候会顺序执行:
17             MethodDe2 methodDe2 = method1;
18             methodDe2 += Program.method2;
19             methodDe2();

 

 1         /// <summary>
 2         /// 调用delegate的时候,发生错误不会返回数值,固用try catch throw:
 3         /// </summary>
 4         /// <param name="n1"></param>
 5         /// <param name="n2"></param>
 6         /// <returns></returns>
 7         private delegate int MethodDe3(ref int n1,ref int n2);
 8 
 9         private static int method3(ref int n1, ref int n2)
10         {
11             throw new Exception();
12             Console.WriteLine("methode3");
13             return n1 + n2;
14         }
15 
16             //委托的异常捕捉
17             MethodDe3 methodDe3 = method3;
18             //methodDe3(ref n1,ref n2);

 

 1         /// <summary>
 2         /// 委托的最有用的操作是实现回调,回调是传入函数的方法,在函数执行完时调用该方法
 3         /// 下面是一个直接回调
 4         /// </summary>
 5         /// <param name="message"></param>
 6         private delegate void CallBackDe(string message);
 7 
 8         static private void AddTwoNum(int n1, int n2, CallBackDe callBack)
 9         {
10             int result = n1 + n2;
11             callBack("this result:" + result.ToString());
12         }
13 
14         static private void ResultCallBack(string message)
15         {
16             Console.WriteLine(message);
17         }
18 
19             //委托的回调
20             CallBackDe callBack = ResultCallBack;
21             AddTwoNum(5,3,callBack);    

 

 1         /// <summary>
 2         /// 委托的最有用的操作是实现回调,回调是传入函数的方法,在函数执行完时调用该方法
 3         /// 下面是一个异步回调,最有用
 4         /// </summary>
 5         /// <param name="message"></param>
 6         private delegate int MethodDeleagate(int n1, int n2);
 7         static private int AddTwoNums(int n1, int n2)
 8         {
 9             System.Threading.Thread.Sleep(2000);
10             return n1 + n2;
11         }
12         static private void ResultCB(IAsyncResult ar)
13         {
14             MethodDeleagate del = (MethodDeleagate) ((AsyncResult) ar).AsyncDelegate;
15             int result = del.EndInvoke(ar);
16             Console.WriteLine("Result of addition is:"+result);
17         }
18 
19             //委托的异步回调
20             // 工作原理:
21             //1,首先有两个委托,一个del指向AddTwoNumbs,用来加数,一个callback是AsyncCallback的委托对象,用来等del委托结束后调用
22             //2,然后开始开始调用del委托,并传入必要的两个int参数和额外的callback,null
23             //3,这样子del和main就会成了两个线程,交替执行,
24             //4,而del.BeginInvoke调用返回的result将会成为参数放到 当del完成后调用委托对象的callback里
25             //5,此时callback委托也开始运行了,它得到了del中得到的返回值
26             //6,总结,一定会很有用
27             MethodDeleagate del = AddTwoNums;
28             AsyncCallback callback=new AsyncCallback(ResultCB);
29             
30             Console.WriteLine("invokeing the method asynchronously...");
31             IAsyncResult result = del.BeginInvoke(5, 3, callback, null);
32             Console.WriteLine("continuing whith the execution...");
33             Console.ReadLine();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值