﻿ 一文说通Dotnet的委托_其他_开心洋葱网
• 欢迎访问开心洋葱网站，在线教程，推荐使用最新版火狐浏览器和Chrome浏览器访问本网站，欢迎加入开心洋葱` QQ群`
• 为方便开心洋葱网用户，开心洋葱官网已经开启复制功能！
• 欢迎访问开心洋葱网站，手机也能访问哦~欢迎加入开心洋葱多维思维学习平台` QQ群`
• 如果您觉得本站非常有看点，那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~！
• 由于近期流量激增，小站的ECS没能经的起亲们的访问，本站依然没有盈利，如果各位看如果觉着文字不错，还请看官给小站打个赏~~~~~~~~~~~~~！

2363次浏览

# 一、前言

为了防止不提供原网址的转载，特在这里加上原文链接：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 扫描二维码，关注个人公众号，可以第一时间得到最新的个人文章和内容推送 本文版权归作者所有，转载请保留此声明和原文链接