中
当我尝试通过使用TryToGetValue
方法获得的引用添加委托时,向存储在Dictionary
中的Action
添加委托似乎失败了。
下面是一个重现错误的例子:
void Foo()
{
Console.WriteLine("Foo");
}
void Bar()
{
Console.WriteLine("Bar");
}
Dictionary<int, Action> dic = new Dictionary<int, Action>();
dic[3] = delegate{};
dic[3] += Foo;
Action ac;
if (dic.TryGetValue(3,out ac))
{
Console.WriteLine("Found");
ac += Bar;
}
dic[3]();
输出:
Found
Foo
值已找到,但ac
和dic[3]
似乎是对不同对象的引用(Bar
未打印)。
有人能解释一下发生了什么事吗?out parameter
中究竟填充了什么?既然Action
是类,那么ac
引用不应该指向存储在Dictionary
中的同一对象吗?
您的示例可以简化(排除字典):
void Foo() {
Console.WriteLine("Foo");
}
void Bar() {
Console.WriteLine("Bar");
}
...
Action x = Foo;
Action y = x;
if (Object.ReferenceEquals(x, y))
Console.WriteLine("x was equal to y");
// creates new delegate instance:
// x = x + Bar;
// that is equal to
// x = Action.Combine(x, Bar);
// so x is not equal to y any longer:
// x is a combined delegate (Foo + Bar)
// y is a delegate to Foo (an old version of x)
// such a behavior is typical for operators (+, +=, etc.):
// when we declare "public static MyType operator + (MyType a, MyType b)"
// it means that a new object "c" will be created that'll equal to sum of a and b
x += Bar;
if (Object.ReferenceEquals(x, y))
Console.WriteLine("x is still equal to y");
y();
输出为:
x等于y
Foo
这种行为的原因是在+=运算算法