Class SelfCheckingDictionary<K, V>
This dictionary structure automatically checks for duplicate keys as they are being added to the collection.
Duplicate entries are logged and removed from the final collection.
public class SelfCheckingDictionary<K, V> : IDictionary<K, V>, ICollection<KeyValuePair<K, V>>, IEnumerable<KeyValuePair<K, V>>, IEnumerable
Type Parameters
K- The Key Type
V- The Value Type
- Inheritance
-
SelfCheckingDictionary<K, V>
- Implements
-
IDictionary<K, V>ICollection<KeyValuePair<K, V>>IEnumerable<KeyValuePair<K, V>>
- Inherited Members
- Extension Methods
Constructors
SelfCheckingDictionary(string, IEqualityComparer<K>, Func<K, string>)
Creates a SelfCheckingDictionary<K, V> with an EqualityComparer and an optional ToString function.
public SelfCheckingDictionary(string collectionName, IEqualityComparer<K> equalityComparer, Func<K, string> toLog = null)
Parameters
collectionNamestringequalityComparerIEqualityComparer<K>toLogFunc<K, string>
SelfCheckingDictionary(string, Func<K, string>)
Creates a SelfCheckingDictionary<K, V> with an optional ToString function.
public SelfCheckingDictionary(string collectionName, Func<K, string> toLog = null)
Parameters
Properties
Count
Gets the number of unique entries in the SelfCheckingDictionary<K, V>
public int Count { get; }
Property Value
IsReadOnly
Defaults to false.
public bool IsReadOnly { get; }
Property Value
this[K]
Gets a key value pair from the collection or sets a key value pair into the collection.
When setting, if a key already exists, the previous entry will be discarded.
public V this[K key] { get; set; }
Parameters
keyK- The unique key.
Property Value
- V
- The value corresponding to the key.
Keys
Gets a collection containing the keys in the SelfCheckingDictionary<K, V>
public ICollection<K> Keys { get; }
Property Value
- ICollection<K>
Values
Gets a collection containing the values in the SelfCheckingDictionary<K, V>
public ICollection<V> Values { get; }
Property Value
- ICollection<V>
Methods
Add(KeyValuePair<K, V>)
Add a new entry the collection.
If a duplicate key is found, the new value will be discarded.
public void Add(KeyValuePair<K, V> item)
Parameters
itemKeyValuePair<K, V>- The key value pair.
Add(K, V)
Add a new entry the collection.
If a duplicate key is found, the new value will be discarded.
public void Add(K key, V value)
Parameters
keyK- The unique key.
valueV- The value.
Clear()
Removes all items from the ICollection<T>.
public void Clear()
Exceptions
- NotSupportedException
- The ICollection<T> is read-only.
Contains(KeyValuePair<K, V>)
Determines whether the ICollection<T> contains a specific value.
public bool Contains(KeyValuePair<K, V> item)
Parameters
itemKeyValuePair<K, V>- The object to locate in the ICollection<T>.
Returns
- bool
- true if
itemis found in the ICollection<T>; otherwise, false.
ContainsKey(K)
Determines whether the IDictionary<TKey, TValue> contains an element with the specified key.
public bool ContainsKey(K key)
Parameters
keyK- The key to locate in the IDictionary<TKey, TValue>.
Returns
- bool
- true if the IDictionary<TKey, TValue> contains an element with the key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
CopyTo(KeyValuePair<K, V>[], int)
public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex)
Parameters
arrayKeyValuePair<K, V>[]- The one-dimensional Array that is the destination of the elements copied from ICollection<T>. The Array must have zero-based indexing.
arrayIndexint- The zero-based index in
arrayat which copying begins.
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
arrayIndexis less than 0.- ArgumentException
- The number of elements in the source ICollection<T> is greater than the available space from
arrayIndexto the end of the destinationarray.
GetEnumerator()
Returns an enumerator that iterates through the collection.
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
Returns
- IEnumerator<KeyValuePair<K, V>>
- An enumerator that can be used to iterate through the collection.
Remove(KeyValuePair<K, V>)
Removes the first occurrence of a specific object from the ICollection<T>.
public bool Remove(KeyValuePair<K, V> item)
Parameters
itemKeyValuePair<K, V>- The object to remove from the ICollection<T>.
Returns
- bool
- true if
itemwas successfully removed from the ICollection<T>; otherwise, false. This method also returns false ifitemis not found in the original ICollection<T>.
Exceptions
- NotSupportedException
- The ICollection<T> is read-only.
Remove(K)
Removes the element with the specified key from the IDictionary<TKey, TValue>.
public bool Remove(K key)
Parameters
keyK- The key of the element to remove.
Returns
- bool
- true if the element is successfully removed; otherwise, false. This method also returns false if
keywas not found in the original IDictionary<TKey, TValue>.
Exceptions
- ArgumentNullException
keyis null.- NotSupportedException
- The IDictionary<TKey, TValue> is read-only.
TryGetValue(K, out V)
Gets the value associated with the specified key.
public bool TryGetValue(K key, out V value)
Parameters
keyK- The key whose value to get.
valueV- When this method returns, the value associated with the specified key, if the key is found; otherwise, the default value for the type of the
valueparameter. This parameter is passed uninitialized.
Returns
- bool
- true if the object that implements IDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.