委托三------------多播委托与匿名方法
============================================多播委托
-------------------------------------主程序
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication3{classProgram{staticvoidMain(string[]args){//多播委托只能使用无返回值的委托//当str1执行失败后会阻止后面的方法的执行Action<string>a=str1;a+=str2;a+=str2;a+=str2;a+=str2;StrAndName(a,"文聘");Console.ReadKey();}staticvoidstr1(stringname){Console.WriteLine("看我{0}的厉害",name);}staticvoidstr2(stringname){Console.WriteLine("受死吧!{0}",name);}staticvoidStrAndName(Action<string>action,stringname){action(name);}}}
============================================匿名方法
-------------------------------------主程序
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication4{classProgram{staticvoidMain(string[]args){strings="你好!";//不能使用break,goto,continue,ref,outAction<string>a=delegate(stringstr){Console.WriteLine(s+str);Console.ReadKey();};a("我叫张飞");}}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。