委托

委托让我们可以把函数引用保存在变量中。这就像在 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));
}

最新文章

  1. php如何查看变量是真实被引用
  2. 基于Python的网页文档处理脚本实现
  3. Java使用正则表达式解析LRC歌词文件
  4. vim配置方法
  5. 批量导入Excel存在的问题及解决方案
  6. iOS之Cookie
  7. CSRF verification failed. Request aborted.
  8. Data Binding(数据绑定)用户指南
  9. java基础学习总结三(jdk7新特性、变量(局部变量和成员变量)、常量以及运算符)
  10. Java多线程同步代码块
  11. Tiling
  12. UIViewController、UINavigationController与UITabBarController的整合使用
  13. UVa 10400 - Game Show Math
  14. Spring ——依赖注入配置一些知识点
  15. 基于Java SE集合的充值管理系统
  16. Elasticsearch【快速入门】
  17. bootstrap modal 监听滚动条事件
  18. 03 of learning python
  19. 关于JAVA项目中的常用的异常处理情况
  20. hdu 1057 A + B Again

热门文章

  1. ubuntu php编译安装配置
  2. pyspider 爬虫教程(一):HTML 和 CSS 选择器
  3. 关于Struts2中param的作用
  4. Codeforces 920G List Of Integers 二分 + 容斥
  5. 案子前申請 EVB board (Evaluation Board)
  6. AC日记——宠物收养所 bzoj 1208
  7. Oracle 索引(转)
  8. ARM, X86和MIPS
  9. IntelliJ IDEA安装MongoDB的的数据操作插件
  10. eos wasm虚拟机相关接口定义实现