当前位置:网站首页>C#委托与匿名方法浅析
C#委托与匿名方法浅析
2022-07-26 10:43:00 【aLLLiyyy】
在c# 里面我们可以用委托去调用相同方法签名的方法,仔细想想,也就是相当于c语言的函数指针的概念。因为c#继承了太多c的功能,如结构体,重载运算符,预编译指令,等等。简直就是c语言的变种。
大家记住,就把委托想象成装载相同性质的函数的容器,然后利用容器,去访问你刚刚装在进去的函数,就是这么简单。首先用关键字delegate去定义一个委托容器,如下
public delegate string BuyDelegate(int money);
就相当于定义一个普通的方法,然而没有方法体,为什么没有方法体?因为它本身就是委托别的方法执行逻辑的,所以不需要方法体去实现自己的具体逻辑。
然后我们实现一个静态方法,与上面定义好的委托要有享用的函数参数,函数参数个数,以及 returnType(返回类型)。
public static string BuyCorei3(int money) {
return "core-i3 money:"+money;
}
然后装载这个函数到委托,再用委托实例触发调用 如下
BuyDelegate buy = new BuyDelegate(BuyCorei3);//装载,
buy(1100);//用委托触发函数
好了,就是这么简单,下面贴上完整代码,附带 匿名方法实现委托实例。
using System;
namespace ConsoleApp1
{
public delegate string BuyDelegate(int money);
class Program
{
static void Main(string[] args)
{
BuyCPU(500);//里面封装了委托调用
BuyDelegate buy = delegate (int money)//这个用delegate 关键字 定义的方法相当与匿名方法,可以构建委托对象用。
{
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) {//这个方法可以对所有相同性质的函数做前后拦截,相当于装饰设计模式。
long currentTicks = DateTime.Now.Ticks;
buy(money);
long spendTimes = DateTime.Now.Ticks - currentTicks;
Console.WriteLine("time spend:"+spendTimes);
}
}
}
边栏推荐
- [leetcode daily question 2021/4/23]368. Maximum divisible subset
- 2021-08-12函数递归_和鹏哥学习C语言
- Redis docker instance and data structure
- 344.反转字符串
- 抽象工厂及其改进示例
- Mlx90640 infrared thermal imager temperature sensor module development notes (VI) pseudo color coding of infrared images
- 解决:无法加载文件 C:\Users\user\AppData\Roaming\npm\npx.ps1,因为在此系统上禁止运行脚本 。
- 使用grid实现左中右布局,中间内容自适应
- Anaconda is used on vscode (the environment has been configured)
- 剑指Offer(七):斐波那契数列
猜你喜欢

工厂模式详解
![[leetcode daily question 2021/5/8]1723. The shortest time to complete all work](/img/e7/a48bb5b8a86cbc4cd5b37bb16661a8.png)
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work

Phase 4: one of College Students' vocational skills preparation in advance

kali 查看ip地址
![[leetcode daily question 2021/4/23]368. Maximum divisible subset](/img/0b/32ca862963c842a93f79eaac94fb98.png)
[leetcode daily question 2021/4/23]368. Maximum divisible subset
![[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array](/img/9d/5ce5d4144a9edc3891147290e360d8.png)
[leetcode daily question 2021/8/31] 1109. Flight reservation statistics [medium] differential array

Problems encountered in QRcode QR code (C language)

文案秘籍七步曲至----文献团队协作管理

Issue 7: how do you choose between curling up and lying flat

英语基础句型结构------起源
随机推荐
C language callback function
Issue 8: cloud native -- how should college students learn in the workplace
[leetcode daily question 2021/5/8]1723. The shortest time to complete all work
MySQL速学-2021-09-01
剑指Offer(二十一):栈的压入、弹出序列
Dry goods likeshop takeout order system is open source, 100% open source, no encryption
工厂模式详解
创建EOS账户 Action
.net operation redis list list
剑指Offer(八):跳台阶
C#halcon用户控件崩溃的一种处理方法
toolstrip 去边框
C语言鹏哥20210812C语言函数
px2rem-loader将px转化为rem,适配移动端vant-UI等框架
SAP ABAP 守护进程的实现方式
Build ARM embedded development environment
el-table实现可编辑表格
Flutter编译报错 version of NDK matched the requested version 21.0.6113669. Versions available locally: 2
sigmod 函数与softmax 函数对比
router.push(),router.repalce(),router.go()使用