ConcurrentDictionary的ToDictionary
如果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的更多相关文章
- ConcurrentDictionary线程不安全么,你难道没疑惑,你难道弄懂了么?
前言 事情不太多时,会时不时去看项目中同事写的代码可以作个参考或者学习,个人觉得只有这样才能走的更远,抱着一副老子天下第一的态度最终只能是井底之蛙.前两篇写到关于断点传续的文章,还有一篇还未写出,后续 ...
- .net源码分析 - ConcurrentDictionary<TKey, TValue>
List源码分析 Dictionary源码分析 ConcurrentDictionary源码分析 继上篇Dictionary源码分析,上篇讲过的在这里不会再重复 ConcurrentDictionar ...
- 【数据类型】Dictionary 与 ConcurrentDictionary 待续
Dictionary<TKey, TValue> 泛型类提供了从一组键到一组值的映射.通过键来检索值的速度是非常快的,接近于 O(1),这是因为 Dictionary<TKey, T ...
- 基础才是重中之重~ConcurrentDictionary让你的多线程代码更优美
回到目录 ConcurrentDictionary是.net4.0推出的一套线程安全集合里的其中一个,和它一起被发行的还有ConcurrentStack,ConcurrentQueue等类型,它们的单 ...
- 挖一挖C#中那些我们不常用的东西之系列(1)——ToDictionary,ToLookup
这个系列我们看看C#中有哪些我们知道,但是又不知道怎么用,又或者懒得去了解的东西,比如这篇我们要介绍的toDictionary 和ToLookup. 从图中我们看到有四个ToXXX的方法,其中ToAr ...
- hashset hastable dictionary concurrentdictionary区别
1.HashTable 哈希表(HashTable)表示键/值对的集合.在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现 ...
- C#中字典集合HashTable、Dictionary、ConcurrentDictionary三者区别
C#中HashTable.Dictionary.ConcurrentDictionar三者都表示键/值对的集合,但是到底有什么区别,下面详细介绍 一.HashTable HashTable表示键/值对 ...
- 改进ConcurrentDictionary并行使用的性能
上一篇文章“ConcurrentDictionary 对决 Dictionary+Locking”中,我们知道了 .NET 4.0 中提供了线程安全的 ConcurrentDictionary< ...
- ConcurrentDictionary 对决 Dictionary+Locking
在 .NET 4.0 之前,如果我们需要在多线程环境下使用 Dictionary 类,除了自己实现线程同步来保证线程安全之外,我们没有其他选择. 很多开发人员肯定都实现过类似的线程安全方案,可能是通过 ...
随机推荐
- iOS:等比压缩截图代码
将一幅图片按着需要的尺寸进行等比的压缩和放大,最后再截取需要尺寸部分,不知道说清楚没,反正就那意思吧! +(UIImage *)compressImageWith:(UIImage *)image w ...
- 短小强悍的JavaScript异步调用库
对于博文 20行完成一个JavaScript模板引擎 的备受好评我感到很惊讶,并决定用此文章介绍使用我经常使用的另一个小巧实用的工具.我们知道,在浏览器中的 JavaScript 绝大部分的操作都是异 ...
- spring中Bean的注入类型
1.属性注入 即通过setXxx()方法注入Bean的属性值或依赖对象,由于属性注入方式具有可选择性和灵活性高的优点,因此属性注入是实际应用中最常采用的注入方式. 属性注入要求Bean提供 ...
- 常用的机器学习&数据挖掘知识点【转】
转自: [基础]常用的机器学习&数据挖掘知识点 Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Le ...
- 瞧一瞧,看一看呐,用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!!
瞧一瞧,看一看呐用MVC+EF快速弄出一个CRUD,一行代码都不用写,真的一行代码都不用写!!!! 现在要写的呢就是,用MVC和EF弄出一个CRUD四个页面和一个列表页面的一个快速DEMO,当然是在不 ...
- [搜片神器]使用C#实现DHT磁力搜索的BT种子后端管理程序+数据库设计(开源)
谢谢园子朋友的支持,已经找到个VPS进行测试,国外的服务器:http://www.sosobta.com 大家可以给提点意见... 出售商业网站代码,万元起,非诚勿扰,谢谢. 联系h31h31 a ...
- sql2008安装时提示重启计算机失败解决方法
安装sql 2008的时候,在检测安装环境中有一项是”重新启动计算机”显示的结果是“失败”.上网看了半天,找到了解决方案,虽然我没弄明白具体原因,但问题是解决了,解决方案如下: 一.Windows+R ...
- Implicitly Typed Local Variables
Implicitly Typed Local Variables It happens time and time again: I’ll be at a game jam, mentoring st ...
- 使用mysql触发器脚本,解决流水数据的添加。
1.建立表脚本 CREATE DATABASE `spring` DEFAULT CHARACTER SET utf8; USE `spring`; CREATE TABLE `account` ( ...
- SOAP vs REST
Both methods are used by many of the large players. It's a matter of preference. My preference is RE ...