Dictionary<TKey,TValue>.ValueCollection.ICollection.SyncRoot 属性


获取可用于同步对 ICollection 的访问的对象。Gets an object that can be used to synchronize access to the ICollection.

property System::Object ^ System::Collections::ICollection::SyncRoot { System::Object ^ get(); };
object System.Collections.ICollection.SyncRoot { get; }
object? System.Collections.ICollection.SyncRoot { get; }
member this.System.Collections.ICollection.SyncRoot : obj
 ReadOnly Property SyncRoot As Object Implements ICollection.SyncRoot



用于同步对 ICollection 的访问的对象。An object that can be used to synchronize access to the ICollection. Dictionary<TKey,TValue>.ValueCollection 的默认实现中,此属性始终返回当前实例。In the default implementation of Dictionary<TKey,TValue>.ValueCollection, this property always returns the current instance.



中集合的默认实现 System.Collections.Generic 不同步。Default implementations of collections in System.Collections.Generic are not synchronized.

枚举整个集合本质上不是一个线程安全的过程。Enumerating through a collection is intrinsically not a thread-safe procedure. 若要确保枚举过程中的线程安全性,可以在整个枚举过程中锁定集合。To guarantee thread safety during enumeration, you can lock the collection during the entire enumeration. 若要允许多个线程访问集合以进行读写操作,则必须实现自己的同步。To allow the collection to be accessed by multiple threads for reading and writing, you must implement your own synchronization.

SyncRoot 返回一个对象,该对象可用于同步对 ICollection 的访问。SyncRoot returns an object, which can be used to synchronize access to the ICollection. 仅当所有线程在访问集合之前锁定此对象时,同步才有效。Synchronization is effective only if all threads lock this object before accessing the collection. 下面的代码演示 SyncRoot c #、c + + 和 Visual Basic 的属性的用法。The following code shows the use of the SyncRoot property for C#, C++, and Visual Basic.

ICollection ic = ...;  
lock (ic.SyncRoot) {  
   // Access the collection.  
Dim ic As ICollection = ...  
SyncLock ic.SyncRoot  
   ' Access the collection.  
End SyncLock  
ICollection^ ic = ...;  
try {  
   // Access the collection.  
finally {  

检索此属性的值的运算复杂度为 O(1)。Retrieving the value of this property is an O(1) operation.