KGy SOFT

StringKeyedDictionaryTValue Class

KGy SOFT Core Libraries Help
Represents a string keyed dictionary that can be queried also by StringSegment and ReadOnlySpan (in .NET Core 3.0/.NET Standard 2.1 and above) instances.
See the Remarks section for details.
Inheritance Hierarchy

SystemObject
  KGySoft.CollectionsStringKeyedDictionaryTValue

Namespace:  KGySoft.Collections
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.4.0
Syntax

[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

Type Parameters

TValue
The type of the value.

The StringKeyedDictionaryTValue type exposes the following members.

Constructors

  NameDescription
Public methodStringKeyedDictionaryTValue
Initializes a new instance of the StringKeyedDictionaryTValue class using ordinal comparison.
Public methodStringKeyedDictionaryTValue(StringSegmentComparer)
Initializes a new instance of the StringKeyedDictionaryTValue class using the specified comparer.
Public methodStringKeyedDictionaryTValue(IDictionaryString, TValue, StringSegmentComparer)
Initializes a new instance of the StringKeyedDictionaryTValue class from the specified dictionary using the specified comparer.
Public methodStringKeyedDictionaryTValue(IEnumerableKeyValuePairString, TValue, StringSegmentComparer)
Initializes a new instance of the StringKeyedDictionaryTValue class from the specified collection using the specified comparer.
Public methodStringKeyedDictionaryTValue(Int32, StringSegmentComparer)
Initializes a new instance of the StringKeyedDictionaryTValue class using the specified capacity and comparer.
Protected methodStringKeyedDictionaryTValue(SerializationInfo, StreamingContext)
Initializes a new instance of the StringKeyedDictionaryTValue class from serialized data.
Top
Properties

  NameDescription
Public propertyCount
Gets number of elements currently stored in this StringKeyedDictionaryTValue instance.
Public propertyItemReadOnlySpanChar
Gets the value associated with the specified key.
Public propertyItemString
Gets or sets the value associated with the specified key.
Public propertyItemStringSegment
Gets the value associated with the specified key.
Public propertyKeys
Gets the keys stored in the dictionary.
Public propertyValues
Gets the values stored in the dictionary.
Top
Methods

  NameDescription
Public methodAdd
Adds an element with the provided key and value to the StringKeyedDictionaryTValue.
Public methodClear
Removes all keys and values from the StringKeyedDictionaryTValue.
Public methodContainsKey(ReadOnlySpanChar)
Determines whether this instance contains an element with the specified key.
Public methodContainsKey(String)
Determines whether the StringKeyedDictionaryTValue contains a specific key.
Public methodContainsKey(StringSegment)
Determines whether this instance contains an element with the specified key.
Public methodContainsValue
Determines whether the StringKeyedDictionaryTValue contains a specific value.
Public methodGetEnumerator
Returns an enumerator that iterates through the StringKeyedDictionaryTValue.
Protected methodGetObjectData
In a derived class populates a SerializationInfo with the additional data of the derived type needed to serialize the target object.
Public methodGetValueOrDefault(ReadOnlySpanChar)
Tries to get the value from the dictionary for the given key.
Public methodGetValueOrDefault(String)
Tries to get the value from the dictionary for the given key.
Public methodGetValueOrDefault(StringSegment)
Tries to get the value from the dictionary for the given key.
Public methodGetValueOrDefaultTActualValue(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.
Public methodGetValueOrDefaultTActualValue(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.
Public methodGetValueOrDefaultTActualValue(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.
Public methodGetValueOrDefaultTActualValue(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.
Public methodGetValueOrDefaultTActualValue(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.
Public methodGetValueOrDefaultTActualValue(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.
Protected methodOnDeserialization
In a derived class restores the state the deserialized instance.
Public methodRemove
Removes the value with the specified key from the StringKeyedDictionaryTValue.
Public methodTryGetValue(ReadOnlySpanChar, TValue)
Tries to get the value associated with the specified key.
Public methodTryGetValue(String, TValue)
Tries to gets the value associated with the specified key.
Public methodTryGetValue(StringSegment, TValue)
Tries to get the value associated with the specified key.
Top
Extension Methods

  NameDescription
Public Extension MethodAddRangeKeyValuePairString, TValue (Defined by CollectionExtensions.)
Public Extension MethodAsThreadSafeKeyValuePairString, TValue (Defined by CollectionExtensions.)
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
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.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)
Public Extension MethodForEachKeyValuePairString, TValue
Similarly to the List<T>.ForEach method, processes an action on each element of an enumerable collection.
(Defined by EnumerableExtensions.)
Public Extension MethodGetRandomElementKeyValuePairString, TValue(Boolean)Overloaded.
Gets a random element from the enumerable source using a new FastRandom instance.
(Defined by EnumerableExtensions.)
Public Extension MethodGetRandomElementKeyValuePairString, TValue(Random, Boolean)Overloaded.
Gets a random element from the enumerable source using a specified Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodIndexOf(FuncObject, Boolean)Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOf(Object)Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOfKeyValuePairString, TValue(FuncKeyValuePairString, TValue, Boolean)Overloaded.
Searches for an element in the source enumeration where the specified predicate returns .
(Defined by EnumerableExtensions.)
Public Extension MethodIndexOfKeyValuePairString, TValue(KeyValuePairString, TValue)Overloaded.
Searches for an element in the source enumeration.
(Defined by EnumerableExtensions.)
Public Extension MethodIsNullOrEmptyOverloaded.
Determines whether the specified source is  or empty (has no elements).
(Defined by EnumerableExtensions.)
Public Extension MethodIsNullOrEmptyKeyValuePairString, TValueOverloaded.
Determines whether the specified source is  or empty (has no elements).
(Defined by EnumerableExtensions.)
Public Extension MethodJoinKeyValuePairString, TValue(String)Overloaded.
Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions.)
Public Extension MethodJoinKeyValuePairString, TValue(Char)Overloaded.
Concatenates the items of the source collection into a new string instance using the specified separator between the items.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleKeyValuePairString, TValueOverloaded.
Shuffles an enumerable source (randomizes its elements) using a new FastRandom instance.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleKeyValuePairString, TValue(Int32)Overloaded.
Shuffles an enumerable source (randomizes its elements) using the provided seed with a new FastRandom instance.
(Defined by EnumerableExtensions.)
Public Extension MethodShuffleKeyValuePairString, TValue(Random)Overloaded.
Shuffles an enumerable source (randomizes its elements) using a specified Random instance.
(Defined by EnumerableExtensions.)
Public Extension MethodToCircularListKeyValuePairString, TValue (Defined by EnumerableExtensions.)
Public Extension MethodTryAdd(Object, Boolean, Boolean)Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddKeyValuePairString, TValue(KeyValuePairString, TValue, Boolean, Boolean)Overloaded.
Tries to add the specified item to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddRange(IEnumerable, Boolean, Boolean)Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryAddRangeKeyValuePairString, TValue(IEnumerableKeyValuePairString, TValue, Boolean, Boolean)Overloaded.
Tries to add the specified collection to the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryClear(Boolean, Boolean)Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryClearKeyValuePairString, TValue(Boolean, Boolean)Overloaded.
Tries to remove all elements from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
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.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
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.)
Public Extension MethodTryGetElementAt(Int32, Object, Boolean, Boolean)Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryGetElementAtKeyValuePairString, TValue(Int32, KeyValuePairString, TValue, Boolean, Boolean)Overloaded.
Tries to get an item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsert(Int32, Object, Boolean, Boolean)Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertKeyValuePairString, TValue(Int32, KeyValuePairString, TValue, Boolean, Boolean)Overloaded.
Tries to insert the specified item at the specified index to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertRange(Int32, IEnumerable, Boolean, Boolean)Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryInsertRangeKeyValuePairString, TValue(Int32, IEnumerableKeyValuePairString, TValue, Boolean, Boolean)Overloaded.
Tries to insert the specified collection into the target collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemove(Object, Boolean, Boolean)Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveKeyValuePairString, TValue(KeyValuePairString, TValue, Boolean, Boolean)Overloaded.
Tries to remove the specified item from to the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveAt(Int32, Boolean, Boolean)Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveAtKeyValuePairString, TValue(Int32, Boolean, Boolean)Overloaded.
Tries to remove an item at the specified index from the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveRange(Int32, Int32, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.)
Public Extension MethodTryRemoveRangeKeyValuePairString, TValue(Int32, Int32, Boolean, Boolean)Overloaded.
Tries to remove count amount of items from the specified collection at the specified index.
(Defined by EnumerableExtensions.)
Public Extension MethodTryReplaceRange(Int32, Int32, IEnumerable, Boolean, Boolean)Overloaded.
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.)
Public Extension MethodTryReplaceRangeKeyValuePairString, TValue(Int32, Int32, IEnumerableKeyValuePairString, TValue, Boolean, Boolean)Overloaded.
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.)
Public Extension MethodTrySetElementAt(Int32, Object, Boolean, Boolean)Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Public Extension MethodTrySetElementAtKeyValuePairString, TValue(Int32, KeyValuePairString, TValue, Boolean, Boolean)Overloaded.
Tries to set the specified item at the specified index in the collection.
(Defined by EnumerableExtensions.)
Top
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 3.0/.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 Security Note
When using with ordinal or ordinal ignore case comparison, the StringKeyedDictionaryTValue class does not use randomized hashing for keys no longer than 32 characters. Therefore, it is not recommended to expose it to a public service, which may affect the keys to be stored. But since StringKeyedDictionaryTValue is not thread-safe, it is not recommended to use it in such a service anyway.

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.

Note Note
Please note that when using a non-ordinal comparison, then depending on the targeted platform there might occur new string allocations when using query members with StringSegment or ReadOnlySpan<char> parameter values. See the properties and the Create(CultureInfo, Boolean) method of the StringSegmentComparer class for more details.

Thread Safety

Instance members of this type are not safe for multi-threaded operations.
See Also

Reference