黑马程序员技术交流社区
标题:
C# 委托(delegate) 的小例子 最简单的理解
[打印本页]
作者:
蒋元龙
时间:
2013-9-11 00:13
标题:
C# 委托(delegate) 的小例子 最简单的理解
static void Main(string[] args)
{
Console.WriteLine(Exec(GetSet));//传个方法名(GetSet)参数 给 Exec方法
Console.ReadKey();
}
public delegate string GetResultDelegate(); //定义委托,用于将方法做为参数传给Exec.
public static string Get()
{
return "get";
}
public static string GetTest()
{
return "gettest";
}
public static string GetSet()
{
return "getSet";
}
public static string Exec(GetResultDelegate getResult) //Exec方法,得到委托的 方法名 参数
{
return getResult(); //委托的实现
}
利用委托,可以把一个方法做为另一个方法的参数,直接执行参数就OK了。和javascript的函数有点相似,javascript里面的函数也可以做为另一个函数的参数。javascript中直接可以用函数名做参数,而c#中必须通过委托,转个弯,实现同样的功能!
作者:
许庭洲
时间:
2013-9-11 09:21
值得学习ing!
作者:
杨靖
时间:
2013-9-11 09:40
顶一下!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2