勇哥注:
ConcurrentDictionary 是线程安全字典,它能保证在多线程读写的情况下,数据不会乱套。
其中TryUpdate的第三个参数怎么用呢?
网上大部分都是抄的AI的这个例子,那个out从何而来呢? 这答案基本上是狗屁打胡说。
这第三个参数指的是: 在当前值等于参数3的情况下,才更新新值newvalue,否则更新失败返回false。
比较奇怪的作用,勇哥也想不出为啥这样设计。
见下面的例子:
static void Main(string[] args) { ConcurrentDictionary<string, string> vrBuffer = new ConcurrentDictionary<string, string>(); vrBuffer.TryAdd("1", "t1"); vrBuffer.TryAdd("2", "t2"); vrBuffer.TryAdd("3", "t3"); string newvalue = "t22"; if (vrBuffer.ContainsKey("2")) { bool f1= vrBuffer.TryUpdate("2", newvalue, vrBuffer["2"]); } else { vrBuffer.TryAdd("2", newvalue); } }
本文出自勇哥的网站《少有人走的路》wwww.skcircle.com,转载请注明出处!讨论可扫码加群:


