当前位置:网站首页>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 .
边栏推荐
- Thinking and summary of R & D Efficiency
- 全链路灰度在数据库上我们是怎么做的?
- Redis的三种删除策略以及逐出算法
- Three deletion strategies and eviction algorithm of redis
- Redis 3.0 source code analysis - data structure and object SDS list Dict
- LeetCode_ Bit operation_ Medium_ 260. Number III that appears only once
- js win7透明桌面切换背景开始菜单js特效
- Unity performance optimization scheme arrangement
- 研发效能的思考总结
- 一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
猜你喜欢

JS fly into JS special effect pop-up login box

激光slam:LeGO-LOAM---代码编译安装与gazebo测试

Redis入门二:redhat 6.5安装使用

Use of DDR3 (axi4) in Xilinx vivado (4) incentive design

Voice controlled robot based on ROS (II): implementation of upper computer

超大模型工程化实践打磨,百度智能云发布云原生AI 2.0方案

Unity package exe to read and write excel table files

Talking about canvas and three rendering modes in unity

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

什么是数据中台?数据中台带来了哪些价值?_光点科技
随机推荐
C# 读取 CSV文件内数据,导入DataTable后显示
Oracle library access is slow. Why?
Subcontracting loading of wechat applet
Establishment of flask static file service
Redis入门二:redhat 6.5安装使用
Speech controlled robot based on ROS (I): realization of basic functions
LVM logical volume
prometheus配置alertmanager完整过程
js图表散点图例子
View the thread stack according to the lwtid of opengauss/mogdb.
GIS数据漫谈(六)— 投影坐标系统
[1331. Array serial number conversion]
js可拖拽alert弹窗插件
How can enterprises successfully complete cloud migration?
"When you are no longer a programmer, many things will get out of control" -- talk to SUSE CTO, the world's largest independent open source company
太空射击第11课: Sound and Music
Use of DDR3 (axi4) in Xilinx vivado (4) incentive design
Use of DDR3 (axi4) in Xilinx vivado (3) module packaging
Linxu [permission, sticky bit]
动态规划:背包问题模板代码汇总