日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ConcurrentDictionary的ToDictionary

發(fā)布時間:2023/12/13 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ConcurrentDictionary的ToDictionary 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

如果Value是引用,那么在使用Value.Clear()的時候。會清空Value的所有元素,但是不會改變Value的引用

private static void Main(){try{var concurrentDictionary = new ConcurrentDictionary<string, List<int>>();concurrentDictionary.TryAdd("chuck", new List<int>() {1, 2, 3});concurrentDictionary.TryAdd("lihu", new List<int>() {4, 5, 6});var dictionary = concurrentDictionary.ToDictionary(x => x.Key, x => x.Value);foreach (var list in concurrentDictionary.Values){list.Clear();}foreach (var name in dictionary.Keys){Console.WriteLine(name);var list = dictionary[name];foreach (var number in list){Console.Write("\t{0}",number);}Console.WriteLine();}}catch (Exception ex){while (ex != null){Console.WriteLine(ex.Message);ex = ex.InnerException;}}Console.ReadLine();}

還需要測試下Value是List<Student>的情況?

?

?

internal class Student{internal int Id { get; set; }internal string Name { get; set; }}internal class Program{private static void Main(){try{var concurrentDictionary = new ConcurrentDictionary<string, Student>();concurrentDictionary.TryAdd("chuck", new Student() {Id = 1, Name = "chuck"});concurrentDictionary.TryAdd("lihu", new Student() {Id = 2, Name = "lihu"});var dictionary = concurrentDictionary.ToDictionary(x => x.Key, x => x.Value);foreach (var key in concurrentDictionary.Keys){concurrentDictionary[key].Id = 0;concurrentDictionary[key].Name = "hello world";}foreach (var name in dictionary.Keys){Console.Write(name);Console.Write("\tid={0}\tname={1}", dictionary[name].Id, dictionary[name].Name);Console.WriteLine();}}catch (Exception ex){while (ex != null){Console.WriteLine(ex.Message);ex = ex.InnerException;}}Console.ReadLine();}}

?

?

?

?

?

?Value是值類型的

private static void Main(){try{var concurrentDictionary = new ConcurrentDictionary<string, int>();concurrentDictionary.TryAdd("chuck", 1);concurrentDictionary.TryAdd("lihu",4);var dictionary = concurrentDictionary.ToDictionary(x => x.Key, x => x.Value);foreach (var key in concurrentDictionary.Keys){concurrentDictionary[key] = 0;}foreach (var name in dictionary.Keys){Console.Write(name);Console.Write("\t{0}", dictionary[name]);Console.WriteLine();}}catch (Exception ex){while (ex != null){Console.WriteLine(ex.Message);ex = ex.InnerException;}}Console.ReadLine();}

?

轉(zhuǎn)載于:https://www.cnblogs.com/chucklu/p/4943173.html

總結(jié)

以上是生活随笔為你收集整理的ConcurrentDictionary的ToDictionary的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。