第6章 委托和事件
6.1
下面为用到委托的几种情况
启动线程--在C#中,可以告诉计算机并行运行新的执行序列,这种序列就称为线程,通过System.Threading.Thread的一个实例使用方法Sta
通用类库--在通用类库中执行子任务时,要用到委托。
事件--GUI编程中也经常用盗委托
事例:
void EntryPoint()
{
//do anything;
}
//通过下面的代码执行新线程
Thread NewThread = new Thread();
NewThread.Start(EntryPoint);
委托只是一种特殊的对象类型,器特殊之处在于,我们以前定义的所有对象都包含数据,而委托包含的只是方法的细节。
6.1.1 在C#中申明委托
delegate void VoidOpreation(uint x);
提示:
理解委托的一种好方式是把委托的作用当作是给方法签名指定名称。
6.1.2C#种使用委托
priavte delegate string GetAString();
static void Main(string[] args)
{
int x = 40;
GetAString FirstGetAString = new GetAString(x.toString);
Console.WriteLine("String is "+firstStringMethod());
}