保持快乐,善于表达,敢于创新
c#的delegate的解析.
上一篇 /
下一篇 2008-02-20 11:40:00
/ 个人分类:c#
using System;
public class MyClass //类的表示, 类和函数区别是有class关键字,且无().
{
public int InstantMethod(){ //函数需要定义函数类型, 如此函数是int的类型.
Console.WriteLine("call the instant method");
return 0; //表示退出此函数时,返回值为0.
}
public int StaticMethod(){
Console.WriteLine("call the static method.");
return 0;
}
}
delegate int MyDelegate(); //定义一个代理.类型为int
public class Test
{
static void Main(){
MyClass c=new MyClass(); //创建一个新MyClass的类.
MyDelegate d=new MyDelegate(c.InstantMethod); //创建一个新的MyDelegate代理, 并指定代理的方法为MyClass里面的InstantMethod, 这里的c.InstantMethod不能写成c.InstantMethod().
d(); //执行此代理.
d=new MyDelegate(c.StaticMethod);
d();
}
}
收藏
举报
TAG:
delegate