多播委托怪异的行为在C#中? [英] Multicast delegate weird behavior in C#?
问题描述
我有这个简单的事件:
public class ClassA
{
public event Func<string, int> Ev;
public int Do(string l)
{
return Ev(l);
}
}
和方法2:
static int Display(string k)
{
return k.Length;
}
static int Display_2(string k)
{
return k.Length*10;
}
林注册此事件:
Im registering this event :
ClassA a = new ClassA();
a.Ev += Display;
a.Ev += Display_2;
现在,我执行:
Console.WriteLine(a.Do("aaa"));
输出:
什么???
-
他在调用列表中的 2 的方法!它的没有的运行它们,但它为什么只显示从最后的报名结果?
he has in invocation list 2 methods ! it did run them , but why does it shows only the result from the last registration ?
哪里的3
已经造成的? (第一次调用)? (虽然两者显示
+ display_2
被执行死刑......我没想到 console.write
来遍历结果。但也没想到他来决定哪些展示。的)
Where does the result of "3"
has gone ? ( the first invocation ) ? ( although both display
+display_2
was executed... I didn't expect console.write
to iterate through results . but also didn't expect him to decide which to show.)
推荐答案
有在这里玩三个方面:
- 本次活动的实施
- 委托组合的行为
- 在调用它的调用列表中有多个项目委托的行为
有关问题1,你有一个的字段,如的事件。的C#4规范的章节10.8.1给出了一个例子,并规定:
For point 1, you have a field-like event. Section 10.8.1 of the C# 4 spec gives an example, and states that:
外按钮的声明
类,在点击
成员只能在左侧赛德隆使用的 + =
和 = -
运营商,如
Outside the declaration of the
Button
class, theClick
member can be used only on the left-hand saide of the+=
and-=
operators, as in
b.Click += new EventHandler(...);
其中追加委托的调用列表的点击
事件
(重点煤矿)。该规范还明确指出,现场般的事件创建一个委托场,这是从的用在的类进行调用。
(emphasis mine). The spec also makes it clear that a field-like event creates a delegate field, which is used from within the class for invocation.
更一般(2点),通过关于委托组合的C#4规格的会谈节7.8.4 +
和 + =
:
More generally (point 2), section 7.8.4 of the C# 4 spec talks about delegate combination via +
and +=
:
委托组合。每个委托类型都隐式提供下列predefined运营商,其中 D
是委托类型:
Delegate combination. Every delegate type implicitly provides the following predefined operator, where
D
is the delegate type:
D operator +(D x, D y)
二进制 +
operato执行委托组合当两个操作数是一些委托类型的 D
。 [...跳过位,其中 X
或是
是空的......否则,操作的结果是新的委托的调用时,调用第一个操作数,然后调用第二个操作数
The binary +
operato performs delegate combination when both operands are of some delegate type D
. [... skip bits where x
or y
are null ...] Otherwise, the result of the operation is a new delegate that, when invoked, invokes the first operand and then invokes the second operand.
(同样,重点煤矿。)
最后,3点 - 事件调用和返回值。的C#规范规定第15.4节:
Finally, point 3 - event invocation and return values. Section 15.4 of the C# spec states:
如果该委托调用包含输出参数或返回值,它们的最终价值将来自于列表中最后一个委托的调用。
If the delegate invocation includes output parameters or a return value, their final value will come from the invocation of the last delegate in the list.
更一般地,取决于事件的实施。如果你使用一个事件的实现,它使用了正常的委托组合/拆卸步骤,一切都得到了保证。如果你开始编写自定义的实现,它确实疯狂的事情,那是另一回事。
More generally, it depends on the event implementation. If you use an event implementation which uses the "normal" delegate combination/removal steps, everything is guaranteed. If you start writing a custom implementation which does crazy things, that's a different matter.
这篇关于多播委托怪异的行为在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!