Dictionary<int, AAA> kv = new Dictionary<int, AAA> { { 1, new AAA() { Val = "abc" } } }; if (kv.TryGetValue(1, out AAA val)) { val = new AAA() { Val = "efg" }; //这样是错误的 kv[1] = new AAA() { Val = "efg" }; //这样才是对的 }
Dictionary<int, AAA> kv = new Dictionary<int, AAA> { { 1, new AAA() { Val = "abc" } } }; if (kv.TryGetValue(1, out AAA val)) { val = new AAA() { Val = "efg" }; //这样是错误的 kv[1] = new AAA() { Val = "efg" }; //这样才是对的 }