一、前言

为了防止不提供原网址的转载，特在这里加上原文链接：https://www.cnblogs.com/tiger-wang/p/14330314.html

二、简单委托

void PrintInfo(string message);


void Delegate_PrintInfo(string message);


Delegate_PrintInfo = PrintInfo;


public delegate int Delegate_Method(int x, int y);


public delegate void Demo_Func1(string para);
public delegate ClassA Demo_Func2(ClassB para);
private delegate StructA Demo_Func3(int para);


class Program
{
public delegate int Delegate_Method(int x, int y);

static void Main(string[] args)
{
Delegate_Method handler = Sum;
int result = handler(3, 4);
}
static int Sum(int x, int y)
{
return x + y;
}
}


public delegate void Delegate_Method(int x, int y);

class ExampleClass
{
public void Sum(int x, int y)
{
Console.WriteLine(x + y);
}
public void Sub(int x, int y)
{
Console.WriteLine(x - y);
}
}
class Program
{
static void Main(string[] args)
{
ExampleClass example = new ExampleClass();
Delegate_Method delegate_1;
Delegate_Method delegate_2;

delegate_1 = example.Sum;
delegate_2 = example.Sub;

delegate_1(100, 50);
delegate_2(100, 50);
}
}


三、委托链

public delegate void Delegate_Method(int x, int y);

class ExampleClass
{
public void Sum(int x, int y)
{
Console.WriteLine(x + y);
}
public void Sub(int x, int y)
{
Console.WriteLine(x - y);
}
}
class Program
{
static void Main(string[] args)
{
ExampleClass example = new ExampleClass();
Delegate_Method[] delegate_list = new Delegate_Method[] { example.Sum, example.Sub };
Delegate_Method delegate_chain = delegate_list[0] + delegate_list[1];

delegate_chain(100, 50);
}
}


Delegate_Method delegate_chain = delegate_list[0] + delegate_list[1];
Delegate_Method delegate_chain1 = delegate_chain - delegate_list[0];


public delegate void Delegate_Method(int x, int y);

class ExampleClass
{
public void Sum(int x, int y)
{
Console.WriteLine(x + y);
}
public void Sub(int x, int y)
{
Console.WriteLine(x - y);
}
}
class Program
{
static void Main(string[] args)
{
ExampleClass example = new ExampleClass();
Delegate_Method[] delegate_list = new Delegate_Method[] { example.Sum, example.Sub };
Delegate_Method delegate_chain = delegate_list[0] + delegate_list[1];

Delegate[] delegates = delegate_chain.GetInvocationList();
for (int i = 0; i < delegates.Length; i++)
{
Delegate_Method _delegate = (Delegate_Method)delegates[i];
_delegate(100, 50);
}
}
}


四、多播委托

class Program
{
public delegate void Delegate_Method(int x, int y);

public static void Sum(int i, int j)
{
Console.WriteLine(i + j);
}
public static void Sub(int i, int j)
{
Console.WriteLine(i - j);
}

static void Main(string[] args)
{
Delegate_Method delegate1, delegate2, delegate3, delegate4;

delegate1 = Sum;
delegate2 = Sub;

delegate3 = delegate1 + delegate2;
delegate3(100, 50);

delegate4 = delegate3 - delegate2;
delegate4(100, 50);
}
}


五、结论

• 委托是面向对象的操作，类型安全，数据安全；
• 委托派生自Dotnet的Delegate类，它是一个类；
• 委托类型是密封（sealed）的，所以不能从委托继承。
 微信公众号：老王Plus 扫描二维码，关注个人公众号，可以第一时间得到最新的个人文章和内容推送 本文版权归作者所有，转载请保留此声明和原文链接
posted @ 2021-01-27 09:09  老王Plus  阅读(786)  评论(5编辑  收藏  举报