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

 private static void Main()
{
try
{
var concurrentDictionary = new ConcurrentDictionary<string, List<int>>();
concurrentDictionary.TryAdd("chuck", new List<int>() {, , });
concurrentDictionary.TryAdd("lihu", new List<int>() {, , }); 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 = , Name = "chuck"});
concurrentDictionary.TryAdd("lihu", new Student() {Id = , Name = "lihu"}); var dictionary = concurrentDictionary.ToDictionary(x => x.Key, x => x.Value);
foreach (var key in concurrentDictionary.Keys)
{
concurrentDictionary[key].Id = ;
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", );
concurrentDictionary.TryAdd("lihu",); var dictionary = concurrentDictionary.ToDictionary(x => x.Key, x => x.Value);
foreach (var key in concurrentDictionary.Keys)
{
concurrentDictionary[key] = ;
} 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();
}

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. 在IOS中使用json

    1.从https://github.com/stig/json-framework/中下载json框架:json-framework 2.解压下载的包,将class文件夹下的所有文件导入到当前工程下. ...

  2. IME日语输入法的快捷键

    <1>小小技巧 alt+shift可以在中,英,日之间切换 ALT+~可以在假名和英文之间切换 ctrl+CAPSLOCK 和 alt+CAPSLOCK可以在平假名和片假名之间切换 敲完字 ...

  3. Error: Most middleware (like bodyParser) ...

    运行NodeJS时出现如下错误: Error: Most middleware (like bodyParser) is no longer bundled with Express and must ...

  4. 好项目烂架构的问题,四年coder的吐槽

    四年多码农,毕业后在一家小私企做前端:(初始asp.net,对oo有了比较深切的理解:处于对某空间的效仿,对前端技术架构理解的比较透彻): 在这家公司混了4个月之后跳出来想自己单干: 自己接了个小项目 ...

  5. 【BZOJ】【2208】【JSOI2010】连通数

    题解: 1.Tarjan缩点以后对每个连通分量进行深搜,看能到哪些连通分量,能到达的所有连通分量的size之和记为sum.则第i个连通分量对答案的贡献为size[i]*sum(到其他连通分量)+siz ...

  6. cg 到hlsl的转换

    http://msdn.microsoft.com/en-us/library/windows/desktop/ff471376(v=vs.85).aspx http://gamedev.stacke ...

  7. 提升SQL Server速度整理索引碎片

    转载:http://wenku.baidu.com/view/f64c8a707fd5360cba1adbea.html SQL Server2005索引碎片分析和解决方法   毫无疑问,给表添加索引 ...

  8. poj 3170

    两遍bfs ~ #include <cstdio> #include <cstdlib> //#include <cmath> #include <map&g ...

  9. [YY题]HDOJ5288 OO’s Sequence

    题意:求这个式子 $\sum \limits_{i=1}^{n} \sum \limits_{j=1}^{m} f(i, j) mod (10^9 + 7)$ 的值 就是对每个区间[i, j]枚举区间 ...

  10. 无法将 grub-efl-amd64-signed 软件包安装/target/ 中

    64位win7下U盘安装64位ubuntu12.04,出现[无法将 grub-efl-amd64-signed 软件包安装/target/ 中]的错误 1. 已经确认为 12.04.2 的 Bug B ...