StringKeyedDictionaryTValue Class

Represents a string keyed dictionary that can be queried also by StringSegment and ReadOnlySpanT (in .NET Core 2.1/.NET Standard 2.1 and above) instances.

Definition

Namespace: KGySoft.Collections
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 9.0.0-preview.1
C#
[SerializableAttribute]
public class StringKeyedDictionary<TValue> : IStringKeyedDictionary<TValue>, 
	IDictionary<string, TValue>, ICollection<KeyValuePair<string, TValue>>, 
	IEnumerable<KeyValuePair<string, TValue>>, IEnumerable, IDictionary, 
	ICollection, IStringKeyedReadOnlyDictionary<TValue>, IReadOnlyDictionary<string, TValue>, 
	IReadOnlyCollection<KeyValuePair<string, TValue>>, ISerializable, IDeserializationCallback
Inheritance
Object    StringKeyedDictionaryTValue
Implements
IStringKeyedDictionaryTValue, IStringKeyedReadOnlyDictionaryTValue, ICollectionKeyValuePairString, TValue, IDictionaryString, TValue, IEnumerableKeyValuePairString, TValue, IReadOnlyCollectionKeyValuePairString, TValue, IReadOnlyDictionaryString, TValue, ICollection, IDictionary, IEnumerable, IDeserializationCallback, ISerializable

Type Parameters

TValue
The type of the value.

Remarks

Being as a regular IDictionaryTKey, TValue implementation with string key, the StringKeyedDictionaryTValue class can be populated by keys and values as any regular dictionary. However, as it implements also the IStringKeyedDictionaryTValue interface, it allows accessing its values by using StringSegment (supported on every platform) and ReadOnlySpan<char> (in .NET Core 2.1/.NET Standard 2.1 and above) instances.

The StringKeyedDictionaryTValue class uses a custom hashing, which usually makes it faster than a regular DictionaryTKey, TValue with string key.

  Security Note

Without specifying a comparer, the StringKeyedDictionaryTValue class does not use randomized hashing for keys no longer than 32 characters. If you want to want to expose a StringKeyedDictionaryTValue to a public service, then make sure you use it with a randomized hash comparer (eg. with OrdinalRandomized).

Depending on the context, the StringKeyedDictionaryTValue can return either a value type or reference type enumerator. When used in a C# foreach statement directly, the public StringKeyedDictionaryTValueEnumerator type is used, which is a value type (this behavior is similar to the regular DictionaryTKey, TValue class). But when the enumerator is obtained via the IEnumerableT interface (occurs when using LINQ or extension methods), then the StringKeyedDictionaryTValue returns a reference type to avoid boxing and to provide a better performance.

To use custom string comparison you can pass a StringSegmentComparer instance to the constructors. It allows string comparisons by string, StringSegment and ReadOnlySpan<char> instances. By default, the StringKeyedDictionaryTValue uses case-sensitive ordinal comparison.

Constructors

StringKeyedDictionaryTValue Initializes a new instance of the StringKeyedDictionaryTValue class using ordinal comparison.
StringKeyedDictionaryTValue(StringSegmentComparer) Initializes a new instance of the StringKeyedDictionaryTValue class using the specified comparer.
StringKeyedDictionaryTValue(Boolean, Boolean) Initializes a new instance of the StringKeyedDictionaryTValue class that uses an ordinal comparer that satisfies the specified parameters.
StringKeyedDictionaryTValue(IDictionaryString, TValue, StringSegmentComparer) Initializes a new instance of the StringKeyedDictionaryTValue class from the specified dictionary using the specified comparer.
StringKeyedDictionaryTValue(IEnumerableKeyValuePairString, TValue, StringSegmentComparer) Initializes a new instance of the StringKeyedDictionaryTValue class from the specified collection using the specified comparer.
StringKeyedDictionaryTValue(Int32, StringSegmentComparer) Initializes a new instance of the StringKeyedDictionaryTValue class using the specified capacity and comparer.
StringKeyedDictionaryTValue(SerializationInfo, StreamingContext) Initializes a new instance of the StringKeyedDictionaryTValue class from serialized data.
StringKeyedDictionaryTValue(Int32, Boolean, Boolean) Initializes a new instance of the StringKeyedDictionaryTValue class using the specified capacity and an ordinal comparer that satisfies the specified parameters.

Properties

Comparer Gets the StringSegmentComparer that is used to determine equality of keys for this StringKeyedDictionaryTValue.
Count Gets number of elements currently stored in this StringKeyedDictionaryTValue instance.
ItemReadOnlySpanChar Gets the value associated with the specified key.
ItemString Gets or sets the value associated with the specified key.
ItemStringSegment Gets the value associated with the specified key.
Keys Gets the keys stored in the dictionary.
Values Gets the values stored in the dictionary.

Methods

Add Adds an element with the provided key and value to the StringKeyedDictionaryTValue.
Clear Removes all keys and values from the StringKeyedDictionaryTValue.
ContainsKey(ReadOnlySpanChar) Determines whether this instance contains an element with the specified key.
ContainsKey(String) Determines whether the StringKeyedDictionaryTValue contains a specific key.
ContainsKey(StringSegment) Determines whether this instance contains an element with the specified key.
ContainsValue Determines whether the StringKeyedDictionaryTValue contains a specific value.
GetEnumerator Returns an enumerator that iterates through the StringKeyedDictionaryTValue.
GetObjectData In a derived class populates a SerializationInfo with the additional data of the derived type needed to serialize the target object.
GetValueOrDefault(ReadOnlySpanChar) Tries to get the value from the dictionary for the given key.
GetValueOrDefault(String) Tries to get the value from the dictionary for the given key.
GetValueOrDefault(StringSegment) Tries to get the value from the dictionary for the given key.
GetValueOrDefaultTActualValue(ReadOnlySpanChar, TActualValue) Tries to get the typed value from the dictionary for the given key. The defaultValue parameter can have a more specific type than TValue.
GetValueOrDefaultTActualValue(ReadOnlySpanChar, FuncTActualValue) Tries to get the typed value from the dictionary for the given key. The defaultValueFactory can return an instance of a more specific type than TValue.
GetValueOrDefaultTActualValue(String, TActualValue) Tries to get the typed value from the dictionary for the given key. The defaultValue parameter can have a more specific type than TValue.
GetValueOrDefaultTActualValue(String, FuncTActualValue) Tries to get the typed value from the dictionary for the given key. The defaultValueFactory can return an instance of a more specific type than TValue.
GetValueOrDefaultTActualValue(StringSegment, TActualValue) Tries to get the typed value from the dictionary for the given key. The defaultValue parameter can have a more specific type than TValue.
GetValueOrDefaultTActualValue(StringSegment, FuncTActualValue) Tries to get the typed value from the dictionary for the given key. The defaultValueFactory can return an instance of a more specific type than TValue.
OnDeserialization In a derived class restores the state the deserialized instance.
Remove Removes the value with the specified key from the StringKeyedDictionaryTValue.
TryGetValue(ReadOnlySpanChar, TValue) Tries to get the value associated with the specified key.
TryGetValue(String, TValue) Tries to get the value associated with the specified key.
TryGetValue(StringSegment, TValue) Tries to get the value associated with the specified key.

Extension Methods

AddRangeKeyValuePairString, TValue Adds a collection to the target ICollectionT.
(Defined by CollectionExtensions)
AsThreadSafeKeyValuePairString, TValue Returns a LockingCollectionT, which provides a thread-safe wrapper for the specified collection. This only means that if the members are accessed through the returned LockingCollectionT, then the inner state of the wrapped collection remains always consistent and not that all the multi-threading concerns can be ignored.
See the Remarks section of the LockingCollectionT class for details and some examples.
(Defined by CollectionExtensions)
Convert Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions)
ConvertTTarget Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions)
ForEachKeyValuePairString, TValue Similarly to the List<T>.ForEach method, processes an action on each element of an enumerable collection.
(Defined by EnumerableExtensions)
GetRandomElementKeyValuePairString, TValue Gets a random element from the enumerable source using a new FastRandom instance.
(Defined by EnumerableExtensions)
GetRandomElementKeyValuePairString, TValue Gets a random element from the enumerable source using a specified Random instance.
(Defined by EnumerableExtensions)
In Gets whether item is among the elements of set.
See the Examples section of the generic InT(T, T) overload for an example.
(Defined by ObjectExtensions)
IndexOf Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions)
IndexOf Searches for an element in the source enumeration.
(Defined by EnumerableExtensions)
IndexOfKeyValuePairString, TValue Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions)
IndexOfKeyValuePairString, TValue Searches for an element in the source enumeration.
(Defined by EnumerableExtensions)
IsNullOrEmpty Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions)
IsNullOrEmptyKeyValuePairString, TValue Determines whether the specified source is or empty (has no elements).
(Defined by EnumerableExtensions)
JoinKeyValuePairString, TValue Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions)
JoinKeyValuePairString, TValue Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions)
ShuffleKeyValuePairString, TValue Shuffles an enumerable source (randomizes its elements) using a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleKeyValuePairString, TValue Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleKeyValuePairString, TValue Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions)
ShuffleKeyValuePairString, TValue Shuffles an enumerable source (randomizes its elements) using a specified Random instance.
(Defined by EnumerableExtensions)
ToCircularListKeyValuePairString, TValue Creates a CircularListT from an IEnumerableT.
(Defined by EnumerableExtensions)
ToStringKeyedDictionaryKeyValuePairString, TValue Creates a StringKeyedDictionaryTValue from an IEnumerableT instance using the specified keySelector delegate and a comparer.
(Defined by EnumerableExtensions)
ToStringKeyedDictionaryKeyValuePairString, TValue, TValue Creates a StringKeyedDictionaryTValue from an IEnumerableT instance using the specified key and value selector delegates and a comparer.
(Defined by EnumerableExtensions)
TryAdd Tries to add the specified item to the collection.
(Defined by EnumerableExtensions)
TryAddKeyValuePairString, TValue Tries to add the specified item to the collection.
(Defined by EnumerableExtensions)
TryAddRange Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions)
TryAddRangeKeyValuePairString, TValue Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions)
TryClear Tries to remove all elements from the collection.
(Defined by EnumerableExtensions)
TryClearKeyValuePairString, TValue Tries to remove all elements from the collection.
(Defined by EnumerableExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryGetCount Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions)
TryGetCountKeyValuePairString, TValue Tries to get the number of elements in the source enumeration without enumerating it.
(Defined by EnumerableExtensions)
TryGetElementAt Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions)
TryGetElementAtKeyValuePairString, TValue Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions)
TryInsert Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions)
TryInsertKeyValuePairString, TValue Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions)
TryInsertRange Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions)
TryInsertRangeKeyValuePairString, TValue Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions)
TryRemove Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions)
TryRemoveKeyValuePairString, TValue Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions)
TryRemoveAt Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions)
TryRemoveAtKeyValuePairString, TValue Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions)
TryRemoveRange Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions)
TryRemoveRangeKeyValuePairString, TValue Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions)
TryReplaceRange Tries to remove count amount of items from the target at the specified index, and to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions)
TryReplaceRangeKeyValuePairString, TValue Tries to remove count amount of items from the target at the specified index, and to insert the specified collection at the same position. The number of elements in collection can be different from the amount of removed items.
(Defined by EnumerableExtensions)
TrySetElementAt Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions)
TrySetElementAtKeyValuePairString, TValue Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions)

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also