Study Notes -- Delegate in C#

博客包含内容解释和示例,转载自https://www.cnblogs.com/cheese/archive/2005/05/24/161633.html ,但未给出具体信息技术相关关键信息。

Explanation:

Delegate is like hook in C++.
First define a delegate.
Then attach some methods to the delegate.
And then activate delegate when some events occur; delegate will run the methods attached to it.

Example:

class TestDelegate
{
         //define methods which will be attached to delegate
         public static void ShowMsgStatic()
         {
                   System.Console.WriteLine("TestDelegate.ShowMsgStatic()");
         }
         public void ShowMsgNonStatic()
         {
                   System.Console.WriteLine("TestDelegate.ShowMsgNonStatic()");
         }
 
}
class ClassDelegate
{
         //define a delegate
         public delegate void Action();
         private static Action  theAction;
         public static Action Tester
         {
                   get { return theAction; }
                   set { theAction = value; }
         }
 
         public static void run()
         {
                   if (theAction != null)
                            theAction();
         }
}
 
class ClassCaller
{
    public void Execute()
    {
        //attach some methods to delegate
        ClassDelegate.Tester += new ClassDelegate.Action(TestDelegate.ShowMsgStatic);
        ClassDelegate.Tester += new ClassDelegate.Action(new TestDelegate().ShowMsgNonStatic);
 
        //Call delegate: system will run TestDelegate.ShowMsgStatic and TestDelegate.ShowMsgNonStatic in sequence
        ClassDelegate.run();
    }
}
Notes:
1.         Static or non static methods can be attached to delegate
2.         Usually, delegate is attached and called in different place. (This example just want to demonstrate simply, so includes attach and call in one class)
3.         The methods which will be called by delegate can have parameters and return value, the only constraint is methods should have the same declaration, like:
public int ShowMsg (string str) {return str.Length;}    //define method
public delegate int ActionWithParam(string str);        //define delegate
int i = theActionWithParam("123");                      //call delegate
 
4.         If multiple methods with return value are attached to one delegate, when call the delegate, system just return the value returned by method last called.
5.         Be able to unattach methods from delegate:
ClassDelegate.Action act  = new ClassDelegate.Action(TestDelegate.ShowMsgStatic);
ClassDelegate.Tester += act;
ClassDelegate.run();
ClassDelegate.Tester -= act;           //unattach the method
 
6.         The sequence of calling methods is the same with the sequence of attaching methods to delegate, if the methods are attached in one class. But developers can’t decide the sequence if there are multiple classed in which methods are attached to delegate.
7.         Delegate is used like command pattern, it can store some handle logic at first, and activate them when some events occur.

转载于:https://www.cnblogs.com/cheese/archive/2005/05/24/161633.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值