c#中多播委托的执行顺序
我看了
但是当我尝试使用这段代码时 using System; namespace Wrox.ProCSharp.Delegates { class Program { static void One() { Console.WriteLine("watch when i occur"); throw new Exception("Error in watching"); } static void Two() { Console.WriteLine("count"); } static void Three() { Console.WriteLine("great"); } static void Main() { Action d1 = Two; d1+=Two; d1+=Two; d1+=Two; d1+=One; d1+=Three; d1+=Three; d1+=Three; Delegate[] delegates = d1.GetInvocationList(); foreach (Action d in delegates) try { d1(); } catch(Exception) { Console.WriteLine("Exception Caught"); } } } } 这是我得到的输出 count count count count watch when i occur Exception Caught count count count count watch when i occur Exception Caught count count count count watch when i occur Exception Caught count count count count watch when i occur Exception Caught count count count count watch when i occur Exception Caught count count count count watch when i occur Exception Caught count count count count watch when i occur Exception Caught count count count count watch when i occur Exception Caught 显然,委托是按照我编写的非常指定的顺序执行的,并且没有一个Three()方法在抛出异常的One()方法之前执行. 所以我缺少一些东西或者委托中的实际方法按指定的顺序执行,我从书中读到的东西意味着什么. 解决方法
undefined – 行为被指定为任意的(就像你忘记妻子的生日后会发生什么……)所以取决于任意行为是有潜在危险的.
可能是5年后,微软发布了.NET 7,你的程序结果发生了变化.这是“未定义”的意思,今天没有多少测量值可以让您对下一个版本,甚至您的计算机和Fred的计算机之间感到舒适.所以观察它只是有趣的,但对可靠性没有用.依靠你观察到的东西在技术上是错误的. 有时,供应商会特别记录未定义的内容,以便在实施过程中保持灵活性. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |