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; }
member this.System.Collections.ICollection.SyncRoot : obj
 ReadOnly Property SyncRoot As Object Implements ICollection.SyncRoot

屬性值

Object

可用來同步存取 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.

SyncRoot屬性會傳回可用來同步存取的物件 ICollectionThe 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.

適用於

另請參閱