当前位置:网站首页>C# 委托 delegate 的理解
C# 委托 delegate 的理解
2022-07-28 18:28:00 【旧时量刑】
1.什么是委托
委托就是委托某个方法来实现具体的功能,类似于:甲方委托乙方去实现需求;出租房屋人委托中介为其出租房屋。
委托是一种引用类型,从数据结构上来讲:委托和类一样,都是一直用户自定义类型。
2:委托的实现
委托是对方法的抽象和封装。委托对象实质上代表了方法的引用(即内存地址)他存储的就是一系列具有相同签名和返回类型的方法的地址
可以理解为函数的一个包装,它使得c#中的函数可以作为参数来被传递
使用时遵循三步走原则:定义委托、实例化委托,调用委托
调用委托时,委托包含的方法将被执行
委托有三种方法:命名方法委托、多播委托、匿名委托
命名法委托
1:定义委托
修饰符 delegate 返回值类型 委托名 {参数列表}
public delegate void FirstDelegate ();静态方法的实例化委托
2:实例化委托
委托名 委托对象名=new 委托名{方法名}
FirstDelegate firstDelegate=new FirstDelegate(Test.First);
委托的方法可以是静态方法的名称,也可以是实例化方法的名称
方法:
public class Test
{
public static void First()
{
Console.WriteLine("第一次实现静态方法类的委托");
}
}
3:调用
firstDelegate();实例化方法的委托
2:实例化委托
委托名 委托对象名=new 委托名{方法名}
FirstDelegate firstDelegate=new FirstDelegate(new Test().First);
委托的方法可以是静态方法的名称,也可以是实例化方法的名称
方法:
public class Test
{
public void First()
{
Console.WriteLine("第一次实现实例化方法类的委托");
}
}
3:调用
firstDelegate();多播委托
多播委托就是一个委托中注册多个方法,在注册方法时可以在委托中通过加号或则减号来实现添加或者撤销的方法。
例如 我想买饭吃 又想和奶茶 还想吃蛋糕,我委托一个人完不成,我需要委托三个人帮我完成,例如美团
1:定义委托
public class problem
{
public delegate void BuyDelegate();
static void mian()
{
BuyDelegate buyDelegate=new BuyDelegate(Buy.BuyFood);
buyDelegate+=Buy.BuyCake;
buyDelegate+=Buy.BuyTea;
buyDelegate();
}
}
public class Buy
{
public void BuyFood()
{
Conssole.WriyeLine("买个螺狮粉");
}
public void BuyCake()
{
Conssole.WriyeLine("买个蛋糕");
}
public void BuyTea()
{
Conssole.WriyeLine("买杯奶茶");
}
}匿名委托
匿名委托就是 使用匿名的方法注册在委托上 ,实际上就是委托中通过定义代码块来实现委拖的作用
1:定义委托
修饰符 delegate 返回值类型 委托名(参数列表)
2:实例化委托
委托名 委托对象=delegate
{
代码块;
};
3:调用匿名委托
委托对象(参数列表);1:实现水果买卖
public class program
{
public delegate void BuyFruit(double price,double count)
static void main(string[] arg)
{
BuyFurit buyFruit=delegate
{
Console.WriteLine("水果总价为"+price*count);
};
buyFruit(3,5);
}
}什么时候适用委托
1:委托相当于用方法作为另一方法参数,同时,也可以实现在两个不能直接调用的方法中做桥梁,如在多线程中的跨线程的方法调用就得用委托。
2:委托使得一个方法可以作为另一个方法的参数进行传递,这就是委托最大的作用。使用委托可以将同类型的方法绑定到同一个变量上,当调用此变量时就可以一次调用绑定的方法,很方便。
边栏推荐
- Scheduled backup of MySQL database under Windows system
- How to use pycharm to quickly create a flask project
- 最大交换[贪心思想&单调栈实现]
- 【CodeForces】Educational Codeforces Round 132 (Rated for Div. 2)
- The privatized instant messaging platform protects the security of enterprise mobile business
- [dynamic link library (DLL) initialization example program failed "problem]
- Commands related to obtaining administrator permissions
- Durham High Lord (classic DP)
- 超大模型工程化实践打磨,百度智能云发布云原生AI 2.0方案
- Use of DDR3 (axi4) in Xilinx vivado (2) read write design
猜你喜欢

Use of DDR3 (axi4) in Xilinx vivado (2) read write design

Explain RESNET residual network in detail

【pytorch】LSTM神经网络

Use of DDR3 (axi4) in Xilinx vivado (5) board test

[dynamic link library (DLL) initialization example program failed "problem]

Linxu 【权限,粘滞位】

Introduction to seven kinds of polling (practice link attached)

Raspberry pie 4B parsing PWM
![[C language] guessing numbers game](/img/ac/81a82404618487861b67e35f18d13f.png)
[C language] guessing numbers game

C语言数据 3(2)
随机推荐
Linxu 【权限,粘滞位】
Solve the cookie splitting problem (DP)
CNN convolution neural network learning process (weight update)
C语言简单实例 1
TCP.IP
Raspberry pie 4B deploy yolov5 Lite using ncnn
[fasttext -- Summary notes]
进制及数的表示 2
Teach you how to draw a map with ArcGIS [thermal map]
Solve flask integration_ Error reporting in restplus
Raspberry Pie 3 connected to WiFi
Zfoo adds routes similar to mydog
Use of DDR3 (axi4) in Xilinx vivado (5) board test
[C language] comprehensively analyze the pointer and sort out the pointer knowledge points
C语言数据 3(1)
Use of DDR3 (axi4) in Xilinx vivado (4) incentive design
Basic mathematical knowledge (update)
C language - question brushing column
Quick sort template
DSACTF7月re