Class ValueDictionary<TKey, TValue>
- Namespace
- Badeend.ValueCollections
- Assembly
- Badeend.ValueCollections.dll
An immutable, thread-safe dictionary with value semantics.
public sealed class ValueDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable, IEquatable<ValueDictionary<TKey, TValue>> where TKey : notnull
Type Parameters
TKeyThe type of keys in the dictionary.
TValueThe type of values in the dictionary.
- Inheritance
-
ValueDictionary<TKey, TValue>
- Implements
-
IDictionary<TKey, TValue>ICollection<KeyValuePair<TKey, TValue>>IReadOnlyDictionary<TKey, TValue>IReadOnlyCollection<KeyValuePair<TKey, TValue>>IEnumerable<KeyValuePair<TKey, TValue>>IEquatable<ValueDictionary<TKey, TValue>>
- Inherited Members
- Extension Methods
Remarks
A dictionary provides a mapping from a set of keys to a set of values, contains no duplicate keys, and stores its elements in no particular order.
Constructing new instances can be done using CreateBuilder<TKey, TValue>() or ToBuilder(). For creating ValueDictionaries, ValueDictionary<TKey, TValue>.Builder is generally more efficient than Dictionary<TKey, TValue>.
ValueDictionaries have "structural equality". This means that two dictionaries are considered equal only when their contents are equal. As long as a key or a value is present in a ValueDictionary, its hash code may not change.
The order in which the entries are enumerated is undefined.
Properties
Count
Number of items in the dictionary.
[Pure]
public int Count { get; }
Property Value
Empty
Get a new empty dictionary.
This does not allocate any memory.
[Pure]
public static ValueDictionary<TKey, TValue> Empty { get; }
Property Value
- ValueDictionary<TKey, TValue>
IsEmpty
Shortcut for .Count == 0.
[Pure]
public bool IsEmpty { get; }
Property Value
this[TKey]
Get the value associated with the specified key.
public ref readonly TValue this[TKey key] { get; }
Parameters
keyTKey
Property Value
- TValue
Exceptions
- ArgumentNullException
keyisnull.- KeyNotFoundException
The
keydoes not exist.
Keys
All keys in the dictionary in no particular order.
[Pure]
public ValueDictionary<TKey, TValue>.KeysEnumerator Keys { get; }
Property Value
- ValueDictionary<TKey, TValue>.KeysEnumerator
Values
All values in the dictionary in no particular order.
[Pure]
public ValueDictionary<TKey, TValue>.ValuesEnumerator Values { get; }
Property Value
- ValueDictionary<TKey, TValue>.ValuesEnumerator
Methods
ContainsKey(TKey)
Determines whether this dictionary contains an element with the specified key.
[Pure]
public bool ContainsKey(TKey key)
Parameters
keyTKey
Returns
ContainsValue(TValue)
Determines whether this dictionary contains an element with the specified value.
[Pure]
public bool ContainsValue(TValue value)
Parameters
valueTValue
Returns
Remarks
This performs a linear scan through the dictionary.
Equals(ValueDictionary<TKey, TValue>?)
Returns true when the two dictionaries have identical length and content.
[Pure]
public bool Equals(ValueDictionary<TKey, TValue>? other)
Parameters
otherValueDictionary<TKey, TValue>
Returns
GetEnumerator()
Returns an enumerator for this ValueDictionary<TKey, TValue>.
Typically, you don't need to manually call this method, but instead use
the built-in foreach syntax.
[Pure]
public ValueDictionary<TKey, TValue>.Enumerator GetEnumerator()
Returns
- ValueDictionary<TKey, TValue>.Enumerator
GetHashCode()
Serves as the default hash function.
[Pure]
public override sealed int GetHashCode()
Returns
- int
A hash code for the current object.
GetValueOrDefault(TKey)
Attempt to get the value associated with the specified key.
Returns default when the key was not found.
[Pure]
public ref readonly TValue? GetValueOrDefault(TKey key)
Parameters
keyTKey
Returns
- TValue
GetValueOrDefault(TKey, TValue)
Attempt to get the value associated with the specified key.
Returns defaultValue when the key was not found.
[Pure]
public TValue GetValueOrDefault(TKey key, TValue defaultValue)
Parameters
keyTKeydefaultValueTValue
Returns
- TValue
ToBuilder()
Create a new ValueDictionary<TKey, TValue>.Builder with this dictionary as its initial content. This builder can then be used to efficiently construct a new immutable ValueDictionary<TKey, TValue>.
[Pure]
public ValueDictionary<TKey, TValue>.Builder ToBuilder()
Returns
- ValueDictionary<TKey, TValue>.Builder
Remarks
The capacity of the returned builder may be larger than the size of this dictionary. How much larger exactly is undefined.
ToString()
Get a string representation of the collection for debugging purposes. The format is not stable and may change without prior notice.
[Pure]
public override string ToString()
Returns
TryGetValue(TKey, out TValue)
Attempt to get the value associated with the specified key.
Returns false when the key was not found.
public bool TryGetValue(TKey key, out TValue value)
Parameters
keyTKeyvalueTValue
Returns
Operators
operator ==(ValueDictionary<TKey, TValue>?, ValueDictionary<TKey, TValue>?)
Check for equality.
[Pure]
public static bool operator ==(ValueDictionary<TKey, TValue>? left, ValueDictionary<TKey, TValue>? right)
Parameters
leftValueDictionary<TKey, TValue>rightValueDictionary<TKey, TValue>
Returns
operator !=(ValueDictionary<TKey, TValue>?, ValueDictionary<TKey, TValue>?)
Check for inequality.
[Pure]
public static bool operator !=(ValueDictionary<TKey, TValue>? left, ValueDictionary<TKey, TValue>? right)
Parameters
leftValueDictionary<TKey, TValue>rightValueDictionary<TKey, TValue>