使用out引用将委托添加到操作字典时出错

本文关键字:操作 字典 出错 添加 out 引用 使用 | 更新日期: 2023-09-27 18:20:55

当我尝试通过使用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

值已找到,但acdic[3]似乎是对不同对象的引用(Bar未打印)。

有人能解释一下发生了什么事吗?out parameter中究竟填充了什么?既然Action是类,那么ac引用不应该指向存储在Dictionary中的同一对象吗?

使用out引用将委托添加到操作字典时出错

您的示例可以简化(排除字典):

  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

这种行为的原因是在+=运算算法