ConcurrentDictionary对象

ConcurrentDictionary<int, List<a>> dic = new ConcurrentDictionary<int, List<a>>();
dic.TryAdd(1, null);
List<a> val;
dic.TryGetValue(1, out val);

//可以存在value为null的项

val = new List<a>();
val.Add(new a() {val="1111" });

//此时dic中的key=1的value未改变

List<a> val2;
dic.TryGetValue(1, out val2);

dic.Clear();

dic.TryAdd(1,val2);

val2.Add(new a() { val = "1111" });

//此时key=1中的value以改变

TryGetValue方法为copy,out出的对象为不同的内存空间

TryAdd方法为指针赋值,指向的相同内存空间

class a
{
public string val { get; set; }
}

猜你喜欢

转载自www.cnblogs.com/zwz418/p/10514562.html