SortedList<TKey,TValue>.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. SortedList<TKey,TValue> 的默认实现中,此属性始终返回当前实例。In the default implementation of SortedList<TKey,TValue>, 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.

属性返回一个对象,该对象可用于同步对的ICollection访问。 SyncRootThe SyncRoot property returns an object that 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 {  
   Monitor::Enter(ic->SyncRoot);  
   // Access the collection.  
}  
finally {  
   Monitor::Exit(ic->SyncRoot);  
}  

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

适用于

另请参阅