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

定义

获取一个值,该值指示对 ICollection 的访问是否同步(线程安全)。Gets a value that indicates whether access to the ICollection is synchronized (thread safe).

property bool System::Collections::ICollection::IsSynchronized { bool get(); };
bool System.Collections.ICollection.IsSynchronized { get; }
 ReadOnly Property IsSynchronized As Boolean Implements ICollection.IsSynchronized

属性值

如果对 true 的访问是同步的(线程安全),则为 ICollection;否则为 falsetrue if access to the ICollection is synchronized (thread safe); otherwise, false. Dictionary<TKey,TValue> 的默认实现中,此属性始终返回 falseIn the default implementation of Dictionary<TKey,TValue>, this property always returns false.

实现

注解

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. 即使集合是同步的,其他线程仍可以修改该集合,这可能会导致枚举器引发异常。Even when a collection is synchronized, other threads can still modify the collection, which can cause the enumerator to throw an exception. 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.

属性返回一个对象,该对象可用于同步对的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.

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

适用于

另请参阅