作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我读到了
If you are using multicast delegates, you should be aware that the order in which methods chained to the same delegate will be called is formally undefined. You should, therefore, avoid writing code that relies on such methods being called in any particular order.
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()
之前执行抛出异常的方法。
最佳答案
未定义 - 行为被指定为任意行为(例如在您忘记妻子的生日之后发生的事情......),因此依赖任意行为具有潜在危险。
可能是从现在起 5 年后,Microsoft 发布 .NET 7 并且您的程序的结果发生了变化。那是“未定义”的意思,今天的任何测量都不会给你下一个版本带来任何安慰,甚至在你的计算机和 Fred 的计算机之间。所以观察它只是有趣,但对可靠性没有用处。依靠你观察到的东西在技术上是错误的。
有时,供应商会专门记录未定义的内容,以便在实现中保持灵 active 。
关于c# - c# 中多播委托(delegate)中的执行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24484738/
我是一名优秀的程序员,十分优秀!