Click or drag to resize
SafeDictionaryTKey, TValue Class
Represents a thread-safe collection of key/value pairs that can be modified by multiple threads concurrently.
Inheritance Hierarchy

Namespace: CMS.Base
Assembly: CMS.Base (in CMS.Base.dll) Version: 10.0.0
public class SafeDictionary<TKey, TValue> : Hashtable, 
	ICloneThreadItem, IXmlSerializable, IGeneralIndexable<TKey, TValue>, IGeneralIndexable

Type Parameters

Key type
Value type

The SafeDictionaryTKey, TValue type exposes the following members.

Protected fieldmDefaultValue
Default value.
Protected fieldmNullValue
Null value.
Protected fieldmUseWeakReferences
If true, the weak references are used for the items so the memory can be cleared upon request
Public propertyAllowNulls
If true, the dictionary allows null values as valid.
Public propertyCopyToNewThread
If true, the dictionary is copied (not cloned) to a new thread
Public propertyDefaultValue
Default value.
Public propertyIsEmpty
Returns true if the dictionary is empty
Public propertyIsSynchronized
Returns true if dictionary is synchronized.
(Overrides HashtableIsSynchronized.)
Public propertyItem
Items indexer. Gets or sets the value in the dictionary.
Public propertyKeys
Gets an Collection containing the keys in the System.Collections.Hashtable.
(Overrides HashtableKeys.)
Public propertyNullValue
Null value.
Public propertyCode exampleSyncRoot
Gets an object that can be used to synchronize access to the SafeDictionaryTKey, TValue.
(Overrides HashtableSyncRoot.)
Public propertyTypedKeys
Gets a typed collection of keys in this dictionary
Public propertyTypedValues
Gets a typed collection of values in this dictionary
Public propertyCode exampleUseWeakReferences
If true, the weak references are used for the items so the memory can be cleared upon request. The property can be set only when the dictionary is empty. To ensure thread-safety, you have to perform the check for emptiness and property assignment in a critical section (use SyncRoot for that purpose).
Public propertyValues
Gets a Collection containing the values in the System.Collections.Hashtable.
(Overrides HashtableValues.)
Public methodCode exampleAdd
Adds the value to the dictionary if it does not exist. Updates existing, if it does exist.
(Overrides HashtableAdd(Object, Object).)
Public methodCode exampleAddMultiple
Adds multiple items with same value to the dictionary
Public methodClear
Removes all elements from the System.Collections.Hashtable.
(Overrides HashtableClear.)
Public methodClone
Clones the dictionary
(Overrides HashtableClone.)
Public methodCloneForNewThread
Clones the object for new thread
Public methodContains
Returns true if the dictionary contains the given key
(Overrides HashtableContains(Object).)
Public methodContainsKey
Returns true if the dictionary contains the given key
(Overrides HashtableContainsKey(Object).)
Public methodContainsValue
Returns true if the dictionary contains the given value
(Overrides HashtableContainsValue(Object).)
Protected methodCopyPropertiesTo
Copies the dictionary properties to the target dictionary
Public methodCode exampleCopyTo
Copies the System.Collections.Hashtable elements to a one-dimensional System.Array instance at the specified index.
(Overrides HashtableCopyTo(Array, Int32).)
Protected methodGetInternalValue
Gets the value from the internal dictionary
Public methodGetObjectData
Object serialization.
(Overrides HashtableGetObjectData(SerializationInfo, StreamingContext).)
Public methodCode exampleGetRealCount
Gets the real count of the objects in the dictionary.
Public methodRemove
Removes the element with the specified key from the System.Collections.Hashtable.
(Overrides HashtableRemove(Object).)
Protected methodSetInternalValue
Sets the value in the internal dictionary
Public methodTryGetValue
Tries to get the value, returns true if the retrieval was successful.
Extension Methods

Keep in mind that the operations of this class are thread-safe. However, a sequence of operations inherently cannot be thread-safe. If you want to perform multiple operations as one atomic operation, use the SyncRoot object.

In current implementation some read operations do not require locking (i.e. multiple threads can read even though some other thread has the lock). If your situation requires all the readers to wait when the dictionary is locked by some other thread, consider some other collection suitable for readers-writers synchronization problem.

See Also