列表没有实现SyncRoot!
每个人都使用很多List。我需要迭代此列表,因此我使用已知的 SyncRoot 模式。
最近我在这篇帖子中注意到应该避免使用SyncRoot “嵌入式”线程安全性(每个方法将锁定私有对象,而不使用 SyncRoot 属性公开它)。我可以理解,并且部分同意这一点。
问题是 List
List<int> list = new List<int>()
list.SyncRoot;
给了我以下编译器错误:
错误 CS0117:“System.Collections.Generic.List”不包含“SyncRoot”的定义
...如果这是真的,我如何同步 List
Everyone use lot of List. I need to iterate over this list, so I use the known SyncRoot pattern.
Recently I noticed in this post that the SyncRoot should be avoided in favor of "embedded" thread-safety (each method will lock on an private object without exposing it using SyncRoot property). I can understand it, and partially I agree on that.
The question is that List<T> class doesn't implements the SyncRoot property, even if implements the ICollection interface, which expose the SyncRoot property. I say this bause the code
List<int> list = new List<int>()
list.SyncRoot;
give me the following compiler error:
error CS0117: 'System.Collections.Generic.List' does not contain a definition for 'SyncRoot'
...If this is true, how could I synchronize a public property of type List<T> when iterating over it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它实际上是显式实现的。
这意味着您必须转换为
ICollection
才能使用它。It is actually implemented explicitly.
This means you must cast to
ICollection
to use it.