多播委托的实现


using
System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { class Program { static void hello() { Console.WriteLine("hello"); } static void world() { Console.WriteLine("world!"); } static void Main(string[] args) { Action t = hello;//action类用来封装方法 t += world;//使t指向hello和world两个方法 t -= world;//使t不指向world方法 t -= hello;//使t不指向hello方法,或者说hello方法脱离委托掌控 t();//没有方法的委托,一般会报出未将对象引用到实例的错误 Console.ReadKey(); } } }
多播委托只能得到调用方法的最后一个结果,所以想要调用全部的方法,一般我们把多播委托的返回值设为void
throw new exception
 
 

在hello或world中抛出以上异常,就会发现,一旦一个方法出现异常,整个迭代直接结束。

 

 

猜你喜欢

转载自www.cnblogs.com/thebrave/p/11903486.html