C# 托管
2024-09-07 03:58:06
委托
委托让我们可以把函数引用保存在变量中。这就像在 C++ 中使用 typedef 保存函数指针一样。
委托使用关键字 delegate 声明。看看这个例子,你就能理解什么是委托:
例子:
代码:
delegate int Operation(int val1, int val2);
public int Add(int val1, int val2)
{
return val1 + val2;
}
public int Subtract (int val1, int val2)
{
return val1- val2;
}
public void Perform()
{
Operation Oper;
Console.WriteLine("Enter + or - ");
string optor = Console.ReadLine();
Console.WriteLine("Enter 2 operands"); string opnd1 = Console.ReadLine();
string opnd2 = Console.ReadLine(); int val1 = Convert.ToInt32 (opnd1);
int val2 = Convert.ToInt32 (opnd2); if (optor == "+")
Oper = new Operation(Add);
else
Oper = new Operation(Subtract); Console.WriteLine(" Result = {0}", Oper(val1, val2));
}
最新文章
- php如何查看变量是真实被引用
- 基于Python的网页文档处理脚本实现
- Java使用正则表达式解析LRC歌词文件
- vim配置方法
- 批量导入Excel存在的问题及解决方案
- iOS之Cookie
- CSRF verification failed. Request aborted.
- Data Binding(数据绑定)用户指南
- java基础学习总结三(jdk7新特性、变量(局部变量和成员变量)、常量以及运算符)
- Java多线程同步代码块
- Tiling
- UIViewController、UINavigationController与UITabBarController的整合使用
- UVa 10400 - Game Show Math
- Spring ——依赖注入配置一些知识点
- 基于Java SE集合的充值管理系统
- Elasticsearch【快速入门】
- bootstrap modal 监听滚动条事件
- 03 of learning python
- 关于JAVA项目中的常用的异常处理情况
- hdu 1057 A + B Again