Hashtable to Dictionary<>同步 [英] Hashtable to Dictionary<> syncroot .

查看:129
本文介绍了Hashtable to Dictionary<>同步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hashtables具有syncroot属性,但通用字典不会。如果我有这样的代码:

Hashtables have a syncroot property but generic dictionaries don't. If I have code that does this:

lock (hashtable.Syncroot)
{
....
}

如果我正在删除散列表并更改为通用,我该如何复制字典?

How do I replicate this if I am removing the hashtable and changing to generic dictionaries?

推荐答案

如果你要严格兼容,那么Bryan是正确的。这是在Dictionary上维护您当前语义的最佳方式。

If you are going strictly for compatability then Bryan is correct. This is the best way to maintain your current semantics on top of a Dictionary.

扩展它。 SyncRoot属性没有直接添加到通用字典的原因在于它是一种危险的同步方法。这只是比锁(这个)更好,这是非常危险的,容易出现僵局。这里有几个链接说明为什么这是坏的。

Expanding on it though. The reason the SyncRoot property was not directly added to the generic dictionary is that it's a dangerous way to do synchronization. It's only slighly better than "lock(this)" which is very dangerous and prone to deadlocks. Here are a couple of links that speak to why this is bad.

  • http://blogs.msdn.com/bclteam/archive/2005/03/15/396399.aspx
  • http://blogs.msdn.com/ricom/archive/2006/05/01/587750.aspx

这篇关于Hashtable to Dictionary<>同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆