List<T>.ICollection.SyncRoot List<T>.ICollection.SyncRoot List<T>.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. List<T> 的預設實作中,這個屬性永遠會傳回目前的執行個體。In the default implementation of List<T>, 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.

SyncRoot傳回可用來同步存取的ICollection物件。SyncRoot 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.

適用於

另請參閱