1. What is delegate in C#?

A delegate is an object that knows how to call a method.
A delegate type defines the kind of method that delegate instances can call. Specifically,
it defines the method’s return type and its parameter types.

The followingdefines a delegate type called Transformer:
delegate int Transformer (int x);

Transformer is compatible with any method with an int return type and a single
int parameter, such as this:
static int Square (int x) { return x * x; }
Assigning a method to a delegate variable creates a delegate instance:
Transformer t = Square;

complete code:

public delegate int Transformer (int x);
class Util
{
public static void Transform (int[] values, Transformer t)
{
for (int i = ; i < values.Length; i++)
values[i] = t (values[i]);
}
} class Test
{
static void Main()
{
int[] values = { , , };
Util.Transform (values, Square); // Hook in the Square method
foreach (int i in values)
Console.Write (i + " "); // 1 4 9
}
static int Square (int x) { return x * x; }
}

最新文章

  1. CSS float 浮动属性
  2. HTML DOM 教程
  3. hive中大表join
  4. Netty writeAndFlush() 流程与异步
  5. IOS UITableView下拉刷新和上拉加载功能的实现
  6. JQUERY与JS的区别
  7. C++ this 指针
  8. Linux设置全局代理与yum代理
  9. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a
  10. Abnormal build process termination--解决IDEA启动web项目报错
  11. webpack开发环境和生产环境切换原理
  12. JavaScript基础视频教程总结(071-080章)
  13. 内置系统账户:Local system/Network service/Local Service 区别
  14. vim3
  15. delphi Image 处理
  16. 源代码下载 作者:王先荣(Xianrong Wang)
  17. 神经网络 java包
  18. 用EXCEL批量更改文件名,一个命令就能完成
  19. Android 修改Camera拍照的默认保存路径
  20. leetcode-374-Guess Number Higher or Lower(二分查找)

热门文章

  1. react native redux saga增加日志功能
  2. VS Code开发技巧集锦
  3. 洛谷——P2009 跑步
  4. 【Go】基础语法之接口
  5. 谜题54:Null与Void
  6. 【BZOJ 3993】【SDOI 2015】序列统计
  7. 「2018山东一轮集训」Game
  8. SD 一轮集训 day4 弦形袋鼠
  9. HDU 6035 Colorful Tree(补集思想+树形DP)
  10. bzoj 4195: [Noi2015]程序自动分析