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

  1. private static void Main()
  2. {
  3. try
  4. {
  5. var concurrentDictionary = new ConcurrentDictionary<string, List<int>>();
  6. concurrentDictionary.TryAdd("chuck", new List<int>() {, , });
  7. concurrentDictionary.TryAdd("lihu", new List<int>() {, , });
  8.  
  9. var dictionary = concurrentDictionary.ToDictionary(x => x.Key, x => x.Value);
  10. foreach (var list in concurrentDictionary.Values)
  11. {
  12. list.Clear();
  13. }
  14.  
  15. foreach (var name in dictionary.Keys)
  16. {
  17. Console.WriteLine(name);
  18. var list = dictionary[name];
  19. foreach (var number in list)
  20. {
  21. Console.Write("\t{0}",number);
  22. }
  23. Console.WriteLine();
  24. }
  25.  
  26. }
  27. catch (Exception ex)
  28. {
  29. while (ex != null)
  30. {
  31. Console.WriteLine(ex.Message);
  32. ex = ex.InnerException;
  33. }
  34. }
  35. Console.ReadLine();
  36. }

还需要测试下Value是List<Student>的情况

  1. internal class Student
  2. {
  3. internal int Id { get; set; }
  4. internal string Name { get; set; }
  5. }
  6.  
  7. internal class Program
  8. {
  9. private static void Main()
  10. {
  11. try
  12. {
  13. var concurrentDictionary = new ConcurrentDictionary<string, Student>();
  14. concurrentDictionary.TryAdd("chuck", new Student() {Id = , Name = "chuck"});
  15. concurrentDictionary.TryAdd("lihu", new Student() {Id = , Name = "lihu"});
  16.  
  17. var dictionary = concurrentDictionary.ToDictionary(x => x.Key, x => x.Value);
  18. foreach (var key in concurrentDictionary.Keys)
  19. {
  20. concurrentDictionary[key].Id = ;
  21. concurrentDictionary[key].Name = "hello world";
  22. }
  23.  
  24. foreach (var name in dictionary.Keys)
  25. {
  26. Console.Write(name);
  27. Console.Write("\tid={0}\tname={1}", dictionary[name].Id, dictionary[name].Name);
  28. Console.WriteLine();
  29. }
  30.  
  31. }
  32. catch (Exception ex)
  33. {
  34. while (ex != null)
  35. {
  36. Console.WriteLine(ex.Message);
  37. ex = ex.InnerException;
  38. }
  39. }
  40. Console.ReadLine();
  41. }
  42. }

Value是值类型的

  1. private static void Main()
  2. {
  3. try
  4. {
  5. var concurrentDictionary = new ConcurrentDictionary<string, int>();
  6. concurrentDictionary.TryAdd("chuck", );
  7. concurrentDictionary.TryAdd("lihu",);
  8.  
  9. var dictionary = concurrentDictionary.ToDictionary(x => x.Key, x => x.Value);
  10. foreach (var key in concurrentDictionary.Keys)
  11. {
  12. concurrentDictionary[key] = ;
  13. }
  14.  
  15. foreach (var name in dictionary.Keys)
  16. {
  17. Console.Write(name);
  18. Console.Write("\t{0}", dictionary[name]);
  19. Console.WriteLine();
  20. }
  21.  
  22. }
  23. catch (Exception ex)
  24. {
  25. while (ex != null)
  26. {
  27. Console.WriteLine(ex.Message);
  28. ex = ex.InnerException;
  29. }
  30. }
  31. Console.ReadLine();
  32. }

ConcurrentDictionary的ToDictionary的更多相关文章

  1. ConcurrentDictionary线程不安全么,你难道没疑惑,你难道弄懂了么?

    前言 事情不太多时,会时不时去看项目中同事写的代码可以作个参考或者学习,个人觉得只有这样才能走的更远,抱着一副老子天下第一的态度最终只能是井底之蛙.前两篇写到关于断点传续的文章,还有一篇还未写出,后续 ...

  2. .net源码分析 - ConcurrentDictionary<TKey, TValue>

    List源码分析 Dictionary源码分析 ConcurrentDictionary源码分析 继上篇Dictionary源码分析,上篇讲过的在这里不会再重复 ConcurrentDictionar ...

  3. 【数据类型】Dictionary 与 ConcurrentDictionary 待续

    Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary<TKey, T ...

  4. 基础才是重中之重~ConcurrentDictionary让你的多线程代码更优美

    回到目录 ConcurrentDictionary是.net4.0推出的一套线程安全集合里的其中一个,和它一起被发行的还有ConcurrentStack,ConcurrentQueue等类型,它们的单 ...

  5. 挖一挖C#中那些我们不常用的东西之系列(1)——ToDictionary,ToLookup

    这个系列我们看看C#中有哪些我们知道,但是又不知道怎么用,又或者懒得去了解的东西,比如这篇我们要介绍的toDictionary 和ToLookup. 从图中我们看到有四个ToXXX的方法,其中ToAr ...

  6. hashset hastable dictionary concurrentdictionary区别

    1.HashTable 哈希表(HashTable)表示键/值对的集合.在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现 ...

  7. C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别

    C#中HashTable.Dictionary.ConcurrentDictionar三者都表示键/值对的集合,但是到底有什么区别,下面详细介绍 一.HashTable HashTable表示键/值对 ...

  8. 改进ConcurrentDictionary并行使用的性能

    上一篇文章“ConcurrentDictionary 对决 Dictionary+Locking”中,我们知道了 .NET 4.0 中提供了线程安全的 ConcurrentDictionary< ...

  9. ConcurrentDictionary 对决 Dictionary+Locking

    在 .NET 4.0 之前,如果我们需要在多线程环境下使用 Dictionary 类,除了自己实现线程同步来保证线程安全之外,我们没有其他选择. 很多开发人员肯定都实现过类似的线程安全方案,可能是通过 ...

随机推荐

  1. SQL Server 2008 远程过程调用失败

    今天在写程序的时候,突然间发现数据库连接不上了,打开管理器发现SQL2008出现这样的错误. 非常的郁闷,找了好多方法都没有解决,最后想想是不是应为安装vs2013中的SQL Server Expre ...

  2. Java Day 06

    二维数组 定义: 格式1 int[][] arr = new int[3][2]; 格式2 int[][] arr = new int[3][];//每个一维数组初始化时为null 空指针异常 格式3 ...

  3. UpdateData(false) and UpdateData(true)

    数据更新函数: UpdateData(false); 控件的关联变量的值传给控件并改变控件状态(程序--->EXE) UpdateData(true); 控件的状态传给其关联的变量(EXE--- ...

  4. C# 生成二维码并且在中间加Logo

    今天做项目的时候有个在生成二维码并且在中间加入Logo的需求,动手试了几把,总感觉效果没有之前写的好,就翻出旧代码,果然还是熟悉的味道,生成一张效果图如下 左边是微信里面的,右边是我自己生成的 原理比 ...

  5. K短路

    K短路 用dijsktra+A*启发式搜索当点v第K次出堆的时候,这时候求得的路径是k短路.A*算法有一个启发式函数f(p)=g(p)+h(p), 即评估函数=当前值+当前位置到终点的最短距离g(p) ...

  6. jQuery插件手把手教会(二)

    上次我们将到了简单的jQuery插件,这次我们继续: 面向对象的插件开发 为什么要有面向对象的思维,因为如果不这样,你可能需要一个方法的时候就去定义一个function,当需要另外一个方法的时候,再去 ...

  7. httpsClient实例

    import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.F ...

  8. c#操作剪切板

    C#定义了一个类System.Windows.Forms.Clipboard来简化剪切板操作,这个类有一个静态方法,主要有: Clear 清除剪切板中的所有数据: ContainsData,Conta ...

  9. HDU4804 Campus Design 轮廓线dp

    跟上面那篇轮廓线dp是一样的,但是多了两个条件,一个是在原图上可能有些点是不能放的(即障碍),所以转移的时候要多一个判断color[i][j]是不是等于1什么的,另外一个是我们可以有多的1*1的骨牌, ...

  10. ccflow学习下载网址

    1.ccflow下载:http://ccflow.org/download.aspx 2.说明:http://ccbpm.mydoc.io/ 3.各种文档:bbs.ccflow.org/showtop ...