c# - SyncRoot 如何/为什么隐藏在 Queue<T> 上?

标签 c# c#-4.0 interface

困惑,可能遗漏了一些简单的东西..

我有

var q = new Queue<object>();
lock (q.SyncRoot)
{
    ...
}

我明白了

Queue<T> does not provide a defintion for SyncRoot blah blah...

但是,Queue<T>工具 ICollection它定义了 SyncRoot作为公共(public)属性(property)。

所以,首先,为什么这是隐藏的。其次,如何隐藏正在实现的接口(interface)的属性?

最佳答案

您可以通过 implementing it explicitly 隐藏属性:

    object MyQueue.SyncRoot { get; set; }

隐藏because it's deprecated :

We found the SyncRoot-based synchronization APIs to be insufficiently flexible for most scenarios. The APIs allow for thread safe access to a single member of a collection. The problem is that there are numerous scenarios where you need to lock on multiple operations (for example remove one item and add another). In other words, it’s usually the code that uses a collection that wants to choose (and can actually implement) the right synchronization policy, not the collection itself. We found that SyncRoot is actually used very rarely and in cases where it is used, it actually does not add much value. In cases where it’s not used, it is just an annoyance to implementers of ICollection.

关于c# - SyncRoot 如何/为什么隐藏在 Queue<T> 上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12746772/

相关文章:

c# - 如何将输入事件路由到共享同一区域的多个 Canvas 中的特定 Canvas ?

c++ - 在构造函数中将类对象转换为接口(interface)总是返回 NULL

linux - 点击界面不出来

java - 多态性:为什么使用 "List list = new ArrayList"而不是 "ArrayList list = new ArrayList"?

c# - 正确使用界面?

c# - 将 Xamarin 应用程序部署到设备时,Visual Studio 2015 有时会卡住

c# - linq 查询样式的 DefaultIfEmpty 在哪里

c# - 由于这是一个异步方法,返回表达式的类型必须是 'Data' 而不是 'Task<Data>'

c# - 将日期插入 MySql 表时阻止所有星期三和星期四

WPF 模态对话框进入后台