当我尝试通过使用方法获得的引用添加委托时,似乎将委托添加到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
该值已找到,但似乎ac和dic[3]是对不同对象的引用(Bar未打印)。
谁能解释我发生了什么事?到底是用什么填充的out parameter?由于Action是类,不应该ac引用指向存储在Dictionary?