Class LinearDictionary<TKey, TValue>
An implementation of IDictionary<TKey, TValue> that uses equality operators to identify keys instead of hash codes, can be faster and have less of a memory impact in smaller collections.
public class LinearDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable
Type Parameters
TKeyUnique key in the dictionary.
TValueNon-uniuqe value in the dictionary.
- Inheritance
-
LinearDictionary<TKey, TValue>
- Implements
-
IDictionary<TKey, TValue>ICollection<KeyValuePair<TKey, TValue>>IReadOnlyDictionary<TKey, TValue>IReadOnlyCollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>
- Inherited Members
- Extension Methods
Constructors
LinearDictionary(bool, params KeyValuePair<TKey, TValue>[])
public LinearDictionary(bool isReadOnly, params KeyValuePair<TKey, TValue>[] underlyingItems)
Parameters
isReadOnlyboolunderlyingItemsKeyValuePair<TKey, TValue>[]
LinearDictionary(IEqualityComparer<TKey>?, bool, params KeyValuePair<TKey, TValue>[])
public LinearDictionary(IEqualityComparer<TKey>? comparer, bool isReadOnly, params KeyValuePair<TKey, TValue>[] underlyingItems)
Parameters
comparerIEqualityComparer<TKey>isReadOnlyboolunderlyingItemsKeyValuePair<TKey, TValue>[]
LinearDictionary(IEqualityComparer<TKey>?, params KeyValuePair<TKey, TValue>[])
public LinearDictionary(IEqualityComparer<TKey>? comparer, params KeyValuePair<TKey, TValue>[] underlyingItems)
Parameters
comparerIEqualityComparer<TKey>underlyingItemsKeyValuePair<TKey, TValue>[]
LinearDictionary(IEqualityComparer<TKey>?, int)
public LinearDictionary(IEqualityComparer<TKey>? comparer, int capacity = 0)
Parameters
comparerIEqualityComparer<TKey>capacityint
LinearDictionary(params KeyValuePair<TKey, TValue>[])
public LinearDictionary(params KeyValuePair<TKey, TValue>[] underlyingItems)
Parameters
underlyingItemsKeyValuePair<TKey, TValue>[]
LinearDictionary(int)
public LinearDictionary(int capacity = 0)
Parameters
capacityint
Properties
Capacity
The number of elements this dictionary can store before the underlying storage has to be resized.
public int Capacity { get; set; }
Property Value
Count
public int Count { get; }
Property Value
IsReadOnly
Defines methods to manipulate generic collections.
public bool IsReadOnly { get; }
Property Value
this[TKey]
Gets or sets the element with the specified key.
public TValue this[TKey key] { get; set; }
Parameters
keyTKeyThe key of the element to get or set.
Property Value
- TValue
The element with the specified key.
Exceptions
- ArgumentNullException
keyis null.- KeyNotFoundException
The property is retrieved and
keyis not found.- NotSupportedException
The property is set and the IDictionary<TKey, TValue> is read-only.
Keys
public LinearDictionary<TKey, TValue>.KeyCollection Keys { get; }
Property Value
- LinearDictionary<TKey, TValue>.KeyCollection
Values
public LinearDictionary<TKey, TValue>.ValueCollection Values { get; }
Property Value
- LinearDictionary<TKey, TValue>.ValueCollection
Methods
Add(TKey, TValue)
Adds an element with the provided key and value to the IDictionary<TKey, TValue>.
public void Add(TKey key, TValue value)
Parameters
keyTKeyThe object to use as the key of the element to add.
valueTValueThe object to use as the value of the element to add.
Exceptions
- ArgumentNullException
keyis null.- ArgumentException
An element with the same key already exists in the IDictionary<TKey, TValue>.
- NotSupportedException
The IDictionary<TKey, TValue> is read-only.
Clear()
Removes all items from the ICollection<T>.
public void Clear()
Exceptions
- NotSupportedException
The ICollection<T> is read-only.
ContainsKey(TKey)
Determines whether the IDictionary<TKey, TValue> contains an element with the specified key.
public bool ContainsKey(TKey key)
Parameters
keyTKeyThe 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(Array, int)
Copies the elements of the ICollection to an Array, starting at a particular Array index.
public void CopyTo(Array array, int index)
Parameters
arrayArrayThe one-dimensional Array that is the destination of the elements copied from ICollection. The Array must have zero-based indexing.
indexintThe zero-based index in
arrayat which copying begins.
Exceptions
- ArgumentNullException
arrayis null.- ArgumentOutOfRangeException
indexis less than zero.- ArgumentException
<code class="paramref">array</code> is multidimensional.-or-
The number of elements in the source ICollection is greater than the available space fromindexto the end of the destinationarray.
-or-
The type of the source ICollection cannot be cast automatically to the type of the destinationarray.
CopyTo(KeyValuePair<TKey, TValue>[], int)
Copies the elements of the ICollection<T> to an Array, starting at a particular Array index.
public 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 ICollection<T>. The Array must have zero-based indexing.
arrayIndexintThe 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 LinearDictionary<TKey, TValue>.Enumerator GetEnumerator()
Returns
- LinearDictionary<TKey, TValue>.Enumerator
An enumerator that can be used to iterate through the collection.
GetOrAdd(TKey, out TValue, Func<TValue>)
Add an item if it's key doesn't already exist.
public bool GetOrAdd(TKey key, out TValue existingValue, Func<TValue> factory)
Parameters
keyTKeyexistingValueTValuefactoryFunc<TValue>
Returns
GetOrAdd(TKey, out TValue, Lazy<TValue>)
Add an item if it's key doesn't already exist.
public bool GetOrAdd(TKey key, out TValue existingValue, Lazy<TValue> value)
Parameters
keyTKeyexistingValueTValuevalueLazy<TValue>
Returns
GetOrAdd<TState>(TKey, out TValue, in TState, Func<TState, TValue>)
Add an item if it's key doesn't already exist.
public bool GetOrAdd<TState>(TKey key, out TValue existingValue, in TState state, Func<TState, TValue> value)
Parameters
keyTKeyexistingValueTValuestateTStatevalueFunc<TState, TValue>
Returns
Type Parameters
TState
Remove(TKey)
Removes the element with the specified key from the IDictionary<TKey, TValue>.
public bool Remove(TKey key)
Parameters
keyTKeyThe 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.
TryGetKey(Func<TValue, bool>, out TKey)
Gets a key that passes a predicate.
public bool TryGetKey(Func<TValue, bool> predicate, out TKey key)
Parameters
Returns
TryGetKey(TValue, out TKey)
Gets a key that has a value equal to value.
public bool TryGetKey(TValue value, out TKey key)
Parameters
valueTValuekeyTKey
Returns
TryGetValue(TKey, out TValue)
Gets the value associated with the specified key.
public bool TryGetValue(TKey key, out TValue value)
Parameters
keyTKeyThe key whose value to get.
valueTValueWhen 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.
TryRemove(TKey, out TValue)
Remove an item by it's key and output the old value.
public bool TryRemove(TKey key, out TValue value)
Parameters
keyTKeyvalueTValue