当前位置:网站首页>Understanding of C # delegate
Understanding of C # delegate
2022-07-28 20:49:00 【Old sentencing】
1. What is a delegate
Delegation is to delegate a method to achieve specific functions , Be similar to : Party A entrusts Party B to realize the demand ; The lessor entrusts an intermediary to rent the house for him .
A delegate is a reference type , In terms of data structure : Delegates are like classes , Are user-defined types .
2: The implementation of delegation
Delegation is the abstraction and encapsulation of methods . A delegate object essentially represents a reference to a method ( Memory address ) What he stores is the address of a series of methods with the same signature and return type
It can be understood as a package of functions , It makes the c# Functions in can be passed as parameters
Follow the three-step principle when using : Define the entrusted 、 Instantiation delegation , The delegate
When invoking a delegate , The methods contained in the delegate will be executed
There are three ways to delegate : Naming method delegate 、 The multicast delegate 、 Anonymous delegate
Nomenclature delegation
1: Define the entrusted
Modifier delegate return type Name of Commission { parameter list }
public delegate void FirstDelegate ();Instantiation delegate of static method
2: Instantiation delegation
Name of Commission Delegate object name =new Name of Commission { Method name }
FirstDelegate firstDelegate=new FirstDelegate(Test.First);
The method of a delegate can be the name of a static method , It can also be the name of the instantiation method
Method :
public class Test
{
public static void First()
{
Console.WriteLine(" It is the first time to implement the delegation of static method classes ");
}
}
3: call
firstDelegate();Delegate of instantiation method
2: Instantiation delegation
Name of Commission Delegate object name =new Name of Commission { Method name }
FirstDelegate firstDelegate=new FirstDelegate(new Test().First);
The method of a delegate can be the name of a static method , It can also be the name of the instantiation method
Method :
public class Test
{
public void First()
{
Console.WriteLine(" It is the first time to implement the delegation of instantiation method class ");
}
}
3: call
firstDelegate();The multicast delegate
Multicast delegation is to register multiple methods in a delegation , When registering methods, you can add or revoke methods by adding or subtracting signs in delegates .
for example I want to buy some rice And I want to have milk tea I also want to eat cake , I can't entrust one person , I need to entrust three people to help me finish , For example, meituan
1: Define the entrusted
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(" Buy a snail lion powder ");
}
public void BuyCake()
{
Conssole.WriyeLine(" Buy a cake ");
}
public void BuyTea()
{
Conssole.WriyeLine(" Buy a cup of milk tea ");
}
}Anonymous delegate
Anonymous delegation is Use anonymous methods to register on delegates , In fact, the role of delegating and dragging is realized by defining code blocks in delegating
1: Define the entrusted
Modifier delegate return type Name of Commission ( parameter list )
2: Instantiation delegation
Name of Commission Delegate object =delegate
{
Code block ;
};
3: Call anonymous delegate
Delegate object ( parameter list );1: Realize fruit trading
public class program
{
public delegate void BuyFruit(double price,double count)
static void main(string[] arg)
{
BuyFurit buyFruit=delegate
{
Console.WriteLine(" The total price of fruit is "+price*count);
};
buyFruit(3,5);
}
}When is delegation applicable
1: Delegate is equivalent to using method as another method parameter , meanwhile , You can also bridge two methods that cannot be called directly , For example, cross thread method calls in multithreading have to use delegates .
2: Delegates enable one method to be passed as a parameter to another , This is the greatest function of entrustment . Using delegates, you can bind methods of the same type to the same variable , When this variable is called, the bound method can be called once , Very convenient .
边栏推荐
- 如何平衡SQL中的安全与性能?
- JS fly into JS special effect pop-up login box
- Prize essay solicitation | 2022 cloud native programming challenge draft activity opens
- js win7透明桌面切换背景开始菜单js特效
- Redis review summary
- Use order by to sort
- View the thread stack according to the lwtid of opengauss/mogdb.
- Unity package exe to read and write excel table files
- 3D激光SLAM:LeGO-LOAM论文解读---简介部分
- 想画一张版权属于你的图吗?AI作画,你也可以
猜你喜欢

Thinking and summary of R & D Efficiency

Beautiful blue background form input box style

LVM logical volume

Integrating database Ecology: using eventbridge to build CDC applications

LVS deployment Dr cluster

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

太空射击第15课: 道具

Cartoon JS shooting game source code

【服务器数据恢复】HP StorageWorks系列存储RAID5两块盘故障离线的数据恢复案例

TCP.IP
随机推荐
Lvs+keepalived high availability deployment practical application
Unity object path query tool
Unity package exe to read and write excel table files
Mongoose condition queries part of the data of the specified attribute
JS picture hanging style photo wall JS special effect
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
C# 委托 delegate 的理解
System. ArgumentException: Object of type ‘System. Int64‘ cannot be converted to type ‘System.Int32‘
Raspberry pie 4B uses MNN to deploy yolov5 Lite
PL515 SOT23-5 单/双口 USB 充电协议端口控制器 百盛电子代理商
不懂就问,快速成为容器服务进阶玩家!
H5 wechat shooting game source code
Use of DDR3 (axi4) in Xilinx vivado (3) module packaging
Prize essay solicitation | 2022 cloud native programming challenge draft activity opens
Unity typewriter teaches you three ways
Unity gadget displays the file size of the resource directory
FPGA programming experience
Oracle library access is slow. Why?
LVS deployment Dr cluster
Explain several mobile ways of unity in detail