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
TKey
The type of keys in the dictionary.
TValue
The 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
inner
IDictionary<TKey, TValue>The inner dictionary to wrap.
Exceptions
- ArgumentNullException
Thrown if
inner
is 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
comparer
IEqualityComparer<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
capacity
intThe 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
capacity
intThe initial number of elements that the dictionary can contain.
comparer
IEqualityComparer<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
key
TKeyThe 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
item
KeyValuePair<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
key
TKeyThe key of the element to add.
value
TValueThe 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
item
KeyValuePair<TKey, TValue>The key-value pair to locate in the dictionary.
Returns
- bool
true
if 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
key
TKeyThe key to locate in the dictionary.
Returns
- bool
true
if 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
array
KeyValuePair<TKey, TValue>[]The one-dimensional array that is the destination of the elements copied from the dictionary.
arrayIndex
intThe zero-based index in
array
at 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
item
KeyValuePair<TKey, TValue>The key-value pair to remove.
Returns
- bool
true
if 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
key
TKeyThe key of the element to remove.
Returns
- bool
true
if 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
key
TKeyThe key of the value to get.
value
TValueWhen 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
value
parameter.
Returns
- bool
true
if the dictionary contains an element with the specified key; otherwise,false
.