1

当我尝试通过使用方法获得的引用添加委托时,似乎将委托添加到Action存储中失败。DictionaryTryToGetValue

这是一个重现错误的示例:

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?

4

1 回答 1

1

您的示例可以简化(不包括字典):

  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

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

于 2013-07-04T11:17:18.787 回答