C#基础五

Reading C#高级编程(第四版)……………………………………………………………………………………………………………………………………………………………

 

委托

C++中定义的函数指针,在C#中成了委托,委托是一个C#类型,派生自System.Delegate

定义:private delegate string GetAString();

使用:GetAString fMethord = new GetAStrin(x.ToString);

         fMethor();//same as  "x.ToString()"

 

匿名委托

delegate string delegateTest(string val);

delegateTest anonDel = delegate(string param)

{

    param+=mid;

    param+="add to string now.";

    return param;

}

delegateTest anonDel = new delegateTest(param+=mid;);

 

多播委托

delegate string delegateTest(string val);

delegateTest oper =  new delegateTest(ClassA.MultiplyByTwo);

oper+= new delegateTest(ClassB.MultiplyByTwo);

oper += oper1;

oper -= oper1;

 

事件其实使用委托实现的

 

评注:C#使用委托的方法实现了signal-slot技术,缺点是方式太单一不灵活

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章