Class SynchronizedDictionary<TKey, TValue>
- Namespace
- Ecng.Collections
- Assembly
- Ecng.Collections.dll
Represents a thread-safe dictionary that provides synchronization for its operations.
public class SynchronizedDictionary<TKey, TValue> : ISynchronizedCollection<KeyValuePair<TKey, TValue>>, ISynchronizedCollection, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable
Type Parameters
TKeyThe type of keys in the dictionary.
TValueThe type of values in the dictionary.
- Inheritance
-
SynchronizedDictionary<TKey, TValue>
- Implements
-
ISynchronizedCollection<KeyValuePair<TKey, TValue>>IDictionary<TKey, TValue>ICollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- Derived
- Inherited Members
- Extension Methods
Constructors
SynchronizedDictionary()
Initializes a new instance of the SynchronizedDictionary<TKey, TValue> class.
public SynchronizedDictionary()
SynchronizedDictionary(IDictionary<TKey, TValue>)
Initializes a new instance of the SynchronizedDictionary<TKey, TValue> class with the specified inner dictionary.
protected SynchronizedDictionary(IDictionary<TKey, TValue> inner)
Parameters
innerIDictionary<TKey, TValue>The inner dictionary to wrap.
Exceptions
- ArgumentNullException
Thrown if
inneris null.
SynchronizedDictionary(IEqualityComparer<TKey>)
Initializes a new instance of the SynchronizedDictionary<TKey, TValue> class with the specified equality comparer.
public SynchronizedDictionary(IEqualityComparer<TKey> comparer)
Parameters
comparerIEqualityComparer<TKey>The equality comparer to use when comparing keys.
SynchronizedDictionary(int)
Initializes a new instance of the SynchronizedDictionary<TKey, TValue> class with the specified initial capacity.
public SynchronizedDictionary(int capacity)
Parameters
capacityintThe initial number of elements that the dictionary can contain.
SynchronizedDictionary(int, IEqualityComparer<TKey>)
Initializes a new instance of the SynchronizedDictionary<TKey, TValue> class with the specified initial capacity and equality comparer.
public SynchronizedDictionary(int capacity, IEqualityComparer<TKey> comparer)
Parameters
capacityintThe initial number of elements that the dictionary can contain.
comparerIEqualityComparer<TKey>The equality comparer to use when comparing keys.
Properties
Count
Gets the number of key-value pairs contained in the dictionary.
public int Count { get; }
Property Value
IsReadOnly
Gets a value indicating whether the dictionary is read-only.
public bool IsReadOnly { get; }
Property Value
this[TKey]
Gets or sets the value associated with the specified key.
public virtual TValue this[TKey key] { get; set; }
Parameters
keyTKeyThe key of the value to get or set.
Property Value
- TValue
The value associated with the specified key.
Keys
Gets a collection containing the keys in the dictionary.
public ICollection<TKey> Keys { get; }
Property Value
- ICollection<TKey>
SyncRoot
Gets the synchronization root object used to synchronize access to the dictionary.
public SyncObject SyncRoot { get; }
Property Value
Values
Gets a collection containing the values in the dictionary.
public ICollection<TValue> Values { get; }
Property Value
- ICollection<TValue>
Methods
Add(KeyValuePair<TKey, TValue>)
Adds a key-value pair to the dictionary.
public void Add(KeyValuePair<TKey, TValue> item)
Parameters
itemKeyValuePair<TKey, TValue>The key-value pair to add.
Add(TKey, TValue)
Adds the specified key and value to the dictionary.
public virtual void Add(TKey key, TValue value)
Parameters
keyTKeyThe key of the element to add.
valueTValueThe value of the element to add.
Clear()
Removes all keys and values from the dictionary.
public virtual void Clear()
Contains(KeyValuePair<TKey, TValue>)
Determines whether the dictionary contains a specific key-value pair.
public bool Contains(KeyValuePair<TKey, TValue> item)
Parameters
itemKeyValuePair<TKey, TValue>The key-value pair to locate in the dictionary.
Returns
- bool
trueif the dictionary contains the key-value pair; otherwise,false.
ContainsKey(TKey)
Determines whether the dictionary contains the specified key.
public virtual bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to locate in the dictionary.
Returns
- bool
trueif the dictionary contains an element with the specified key; otherwise,false.
CopyTo(KeyValuePair<TKey, TValue>[], int)
Copies the elements of the dictionary to an array, starting at a particular array index.
public virtual void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
Parameters
arrayKeyValuePair<TKey, TValue>[]The one-dimensional array that is the destination of the elements copied from the dictionary.
arrayIndexintThe zero-based index in
arrayat which copying begins.
GetEnumerator()
Returns an enumerator that iterates through the dictionary.
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<TKey, TValue>>
An enumerator that can be used to iterate through the dictionary.
Remove(KeyValuePair<TKey, TValue>)
Removes the first occurrence of a specific key-value pair from the dictionary.
public bool Remove(KeyValuePair<TKey, TValue> item)
Parameters
itemKeyValuePair<TKey, TValue>The key-value pair to remove.
Returns
- bool
trueif the key-value pair was successfully removed; otherwise,false.
Remove(TKey)
Removes the value with the specified key from the dictionary.
public virtual bool Remove(TKey key)
Parameters
keyTKeyThe key of the element to remove.
Returns
- bool
trueif the element is successfully found and removed; otherwise,false.
TryGetValue(TKey, out TValue)
Gets the value associated with the specified key.
public virtual bool TryGetValue(TKey key, out TValue value)
Parameters
keyTKeyThe key of the value to get.
valueTValueWhen this method returns, contains the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
valueparameter.
Returns
- bool
trueif the dictionary contains an element with the specified key; otherwise,false.