Dictionary<TKey,TValue>.ICollection.SyncRoot Dictionary<TKey,TValue>.ICollection.SyncRoot Dictionary<TKey,TValue>.ICollection.SyncRoot Property

定義

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

實作

備註

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.

屬性會傳回可用來同步存取的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 the 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)運算。Getting the value of this property is an O(1) operation.

適用於

另請參閱