class A { internal int value; }
ConcurrentDictionary<int, A> dic = new ConcurrentDictionary<int, A>(); dic.TryAdd(1, new A() {value = 1}); dic.TryAdd(2, new A() {value = 2}); A a; bool flag; flag = dic.TryRemove(2, out a); Console.WriteLine(flag); if(a == null) Console.WriteLine("a==null"); else { Console.WriteLine($"a.value={a.value}"); } flag = dic.TryRemove(3, out a); Console.WriteLine(flag); if(a == null) Console.WriteLine("a==null");
移除成功
True
a.value=2
移除失败
False
a==null