SortedDictionary<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; }
 ReadOnly Property SyncRoot As Object Implements ICollection.SyncRoot

属性值

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

实现

注解

System.Collections.Generic 命名空间中集合的默认实现是不同步的。Default implementations of collections in the System.Collections.Generic namespace 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.

@No__t-0 属性返回一个对象,该对象可用于同步对 @no__t 的访问。The SyncRoot property returns an object that can be used to synchronize access to the ICollection. 仅当所有线程在访问集合之前锁定对象时,同步才有效。Synchronization is effective only if all threads lock the object before accessing the collection. 下面的代码演示如何对C#、 C++和 Visual Basic 使用 SyncRoot 属性。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   
{  
    Monitor::Enter(ic->SyncRoot);  
    // Access the collection.  
}  
finally   
{  
    Monitor::Exit(ic->SyncRoot);  
}  

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

适用于

另请参阅