C#基础知识学习之 ✨ “精神小伙“——委托(delegate) 的含义和用法
https://blog.csdn.net/zhangay1998/article/details/115697668
C#委托(Delegate)
在C#的学习过程中,自然有我们的精神小伙——委托 的一席之地
本文就介绍一下C#中的委托,这位可以包含同类方法函数的小伙子
C# 中的委托(Delegate)类似于 C 或 C++ 中函数的指针。 委托(Delegate)
是存有对某个方法的引用的一种引用类型变量。引用可在运行时被改变。 委托就是用来储存方法的结构
委托(Delegate)特别用于实现事件和回调方法。所有的委托(Delegate)都派生自 System.Delegate 类。
声明和定义委托分为4个步骤
- 声明一个委托(函数指针)
- 创建委托对象
- 创造符合委托格式的函数(指针指向的函数)
- 将函数名称赋值给委托
Tips:委托对象为空是不能执行的,执行前先提前判断
代码实例如下
// 1. 声明一个委托(函数指针)
delegate void delegateTest(int a,float b);
// delegate 返回值类型 委托类型名(参数列表):
// 2. 创建委托对象
public delegateTest dT1;
//3. 创造符合委托格式的函数(指针指向的函数)
public void Test(int a,float b)
{
a+=100;
b*=2.0;
Console.WriteLine("a:{0},b:{1}", a,b);
}
static void Main(string[] args)
{
// 4. 将函数名称赋值给委托
dT1=new delegateTest(Test);
//使用委托
dT1(1,1.0);
<span class="token comment">//打印结果</span>
<span class="token comment">//a=101,b=2.0</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
委托类型
- 委托/代理:句柄
- 委托是自定义类型
- 委托是引用类型
几种基础的委托结构
- 自定义委托 语法: delegate 返回类型 委托名([参数]);
- Action内置委托 语法: Action<[参数类型0-16个]> 委托名=被委托方法名;
- Func内置委托 语法: Function<[参数类型0-16个],返回类型> 委托名=被委托方法名;
- 多播委托 语法:委托名+=被委托方法名;委托名-=被委托方法名;
委托的用法
下面的实例演示了委托的一个用法,可以将定义的委托作为参数,就可以直接通过委托来调用委托中的方法了。
//声明委托
public delegate void OndelegateTest(int a,int b);
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">TestNum1</span><span class="token punctuation">(</span><span class="token keyword">int</span> i,<span class="token keyword">int</span> j<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token keyword">int</span> num <span class="token operator">=</span> i<span class="token operator">+</span>j<span class="token punctuation">;</span>
Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token string">"num:"</span><span class="token operator">+</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">TestNum2</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">,</span><span class="token keyword">int</span> j<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token keyword">int</span> num <span class="token operator">=</span> i<span class="token operator">*</span>j<span class="token punctuation">;</span>
Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token string">"num:"</span><span class="token operator">+</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 该方法把委托作为参数,并使用它调用方法</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">sendNum</span><span class="token punctuation">(</span><span class="token class-name">OndelegateTest</span> test<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token function">test</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
static void Main(string[] args)
{
OndelegateTest d1=new OndelegateTest(TestNum1);
OndelegateTest d2=new OndelegateTest(TestNum2);
sendNum(d1);
sendNum(d2);
Console.ReadKey();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
委托的多播(Multicasting of a Delegate)
委托对象可使用 “+” 运算符进行合并。一个合并委托调用它所合并的两个委托。只有相同类型的委托可被合并。"-" 运算符可用于从合并的委托中移除组件委托。
使用委托的这个有用的特点,您可以创建一个委托被调用时要调用的方法的调用列表。这被称为委托的 多播(multicasting),也叫组播。下面的程序演示了委托的多播:
using System;
//声明委托
delegate int NumberChanger(int n);
namespace DelegateAppl
{
class TestDelegate
{
static int num = 10;
public static int AddNum(int p)
{
num += p;
return num;
}
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">MultNum</span><span class="token punctuation">(</span><span class="token keyword">int</span> q<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
num <span class="token operator">*</span><span class="token operator">=</span> q<span class="token punctuation">;</span>
<span class="token keyword">return</span> num<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">TestNum</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
num <span class="token operator">-</span><span class="token operator">=</span> i<span class="token punctuation">;</span>
<span class="token keyword">return</span> num<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">getNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token keyword">return</span> num<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">Main</span><span class="token punctuation">(</span><span class="token keyword">string</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
<span class="token comment">// 创建委托实例</span>
<span class="token class-name">NumberChanger</span> nc<span class="token punctuation">;</span>
<span class="token class-name">NumberChanger</span> nc1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NumberChanger</span><span class="token punctuation">(</span>AddNum<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token class-name">NumberChanger</span> nc2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NumberChanger</span><span class="token punctuation">(</span>MultNum<span class="token punctuation">)</span><span class="token punctuation">;</span>
nc <span class="token operator">=</span> nc1<span class="token punctuation">;</span>
nc <span class="token operator">+</span><span class="token operator">=</span> nc2<span class="token punctuation">;</span>
nc <span class="token operator">+</span><span class="token operator">=</span> TestNum<span class="token punctuation">;</span>
<span class="token comment">// 调用多播</span>
<span class="token function">nc</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Console<span class="token punctuation">.</span><span class="token function">WriteLine</span><span class="token punctuation">(</span><span class="token string">"Value of Num: {0}"</span><span class="token punctuation">,</span> <span class="token function">getNum</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
Console<span class="token punctuation">.</span><span class="token function">ReadKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
当上面的代码被编译和执行时,它会产生下列结果:
Value of Num: 70
委托对象的绑定与解绑
delegate int NumberChanger(int n);
NumberChanger nc;
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">TestNum</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">)</span>
<span class="token punctuation">{<!-- --></span>
num <span class="token operator">-</span><span class="token operator">=</span> i<span class="token punctuation">;</span>
<span class="token keyword">return</span> num<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
nc <span class="token operator">+</span><span class="token operator">=</span> TestNum<span class="token punctuation">;</span><span class="token comment">//委托的绑定</span>
nc <span class="token operator">-</span><span class="token operator">=</span> TestNum<span class="token punctuation">;</span><span class="token comment">//委托的解绑</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11