当前位置:网站首页>Analysis of C # delegation and anonymous method
Analysis of C # delegation and anonymous method
2022-07-26 10:50:00 【aLLLiyyy】
stay c# Inside, we can use delegates to call methods with the same method signature , Think carefully , Which is the same thing as c The concept of function pointer of language . because c# Inherited too much c The function of , Like a structure , Overload operator , Precompiling instructions , wait . It is c A variation of language .
Remember , Think of delegates as containers that hold functions of the same nature , Then use the container , Visit the function you just installed , It's that simple . First use the keyword delegate To define a delegate container , as follows
public delegate string BuyDelegate(int money);
It is equivalent to defining a common method , However, there is no method body , Why is there no method body ? Because it delegates other methods to execute logic , Therefore, there is no need for the method body to implement its own specific logic .
Then we implement a static method , The delegate defined above should have function parameters to enjoy , Number of function parameters , as well as returnType( Return type ).
public static string BuyCorei3(int money) {
return "core-i3 money:"+money;
}
Then load this function into the delegate , Then use the delegate instance to trigger the call as follows
BuyDelegate buy = new BuyDelegate(BuyCorei3);// load ,
buy(1100);// Trigger function with delegate
Okay , It's that simple , Here's the full code , Incidental Anonymous methods implement delegate instances .
using System;
namespace ConsoleApp1
{
public delegate string BuyDelegate(int money);
class Program
{
static void Main(string[] args)
{
BuyCPU(500);// It encapsulates the delegate call
BuyDelegate buy = delegate (int money)// This one uses delegate keyword The defined method is equivalent to anonymous method , You can build delegate objects with .
{
return "xiaomi smartphone money:" + money;
};
Console.WriteLine(buy(1250));
Console.ReadKey();
}
public static string BuyDog(int money) {
return "a small dog money:" + money;
}
public static string BuyCorei3(int money) {
return "core-i3 money:"+money;
}
public static string BuyCorei5(int money) {
return "core-i5 money:"+money;
}
public static string BuyCorei7(int money) {
return "core-i7 money:"+money;
}
public static void BuyCPU(int money) {
BuyDelegate buy=null;
if (money < 1000)
{
buy = new BuyDelegate(BuyCorei3);
}
else if (money < 2000)
{
buy = new BuyDelegate(BuyCorei5);
}
else {
buy = new BuyDelegate(BuyCorei7);
}
Console.WriteLine(buy(money));
}
public static void CalculateTime(BuyDelegate buy,int money) {// This method can intercept all functions with the same properties before and after , Equivalent to decorative design pattern .
long currentTicks = DateTime.Now.Ticks;
buy(money);
long spendTimes = DateTime.Now.Ticks - currentTicks;
Console.WriteLine("time spend:"+spendTimes);
}
}
}
边栏推荐
- 1837.K进制表示下的各位数字总和
- C language pengge 20210812c language function
- Common classes (understand)
- 构建ARM嵌入式开发环境
- Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2
- 剑指Offer(四十四):翻转单词顺序序列
- Flutter TextField怎样去除下划线及有焦点时颜色
- How to assemble a registry?
- Anaconda is used on vscode (the environment has been configured)
- 使用float实现左中右布局,中间内容自适应
猜你喜欢

Anaconda is used on vscode (the environment has been configured)

Bash shell学习笔记(五)

Bash shell学习笔记(六)
![Error[Pe147]: declaration is incompatible with '错误问题](/img/4f/57145d78f4dc1fe84d2f271dd9d82f.png)
Error[Pe147]: declaration is incompatible with '错误问题

RT thread learning notes (V) -- edit, download and debug programs

在altium designer中禁用USBJATG

Disable usbjatg in Altium Designer
![[leetcode daily question 2021/2/14]765. Lovers hold hands](/img/be/8639a05c733638bf0b3fdeb11abccf.png)
[leetcode daily question 2021/2/14]765. Lovers hold hands

RT-Thread 学习笔记(一)---配置RT-Thread开发环境

IAR sprintf 浮点 在UCOS 总格式化成0.0的问题
随机推荐
Constructors, method overloads, object arrays, and static
使用Selenium抓取zabbix性能监控图
剑指Offer(五十三):表示数值的字符串
剑指Offer(五):用两个栈实现队列
Flutter报错 Incorrect use of ParentDataWidget When the exception was thrown, this was the stack:
$router和$route的区别
@Notblank, @notnull, @notempty differences and uses
2021-08-14 Sanzi chess
Kali view IP address
Bash shell学习笔记(四)
Pengge C language - minesweeping 2021-08-16
Bigdecimal的加减乘除、比较大小、向上向下取整 和 Bigdecimal的集合累加、判断BigDecimal是否有小数
@The difference and use of jsonformat and @datetimeformat
C#委托与匿名方法浅析
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
Sword finger offer (49): convert a string to an integer
2021-08-12函数递归_和鹏哥学习C语言
构建ARM嵌入式开发环境
mysql20210906
MySQL quick learning notes-2021-08-31