Class DictionaryHelper
- Namespace
- SunamoDictionary
- Assembly
- SunamoDictionary.dll
Helper methods for working with dictionaries. Provides utility methods for adding, creating, aggregating and manipulating dictionary entries.
public class DictionaryHelper
- Inheritance
-
DictionaryHelper
- Inherited Members
- Extension Methods
Methods
AddOrCreateIfDontExists<Key, Value>(Dictionary<Key, List<Value>>, Key, Value)
Adds or creates a dictionary entry with duplicate checking. In addition to method AddOrCreate, this method checks whether value in collection does not already exist.
public static void AddOrCreateIfDontExists<Key, Value>(Dictionary<Key, List<Value>> dictionary, Key key, Value value) where Key : notnull
Parameters
dictionaryDictionary<Key, List<Value>>The dictionary to modify.
keyKeyThe key to add or update.
valueValueThe value to add to the list.
Type Parameters
KeyThe type of the dictionary key.
ValueThe type of the values in the list.
AddOrCreateTimeSpan<Key>(Dictionary<Key, TimeSpan>, Key, DateTime)
Adds or creates a TimeSpan entry from a DateTime value.
public static void AddOrCreateTimeSpan<Key>(Dictionary<Key, TimeSpan> dictionary, Key key, DateTime value) where Key : notnull
Parameters
dictionaryDictionary<Key, TimeSpan>The dictionary to modify.
keyKeyThe key to add or update.
valueDateTimeThe DateTime value to convert to TimeSpan and add.
Type Parameters
KeyThe type of the dictionary key.
AddOrCreateTimeSpan<Key>(Dictionary<Key, TimeSpan>, Key, TimeSpan)
Adds or creates a TimeSpan entry, adding to existing value if key exists.
public static void AddOrCreateTimeSpan<Key>(Dictionary<Key, TimeSpan> dictionary, Key key, TimeSpan value) where Key : notnull
Parameters
dictionaryDictionary<Key, TimeSpan>The dictionary to modify.
keyKeyThe key to add or update.
valueTimeSpanThe TimeSpan value to add.
Type Parameters
KeyThe type of the dictionary key.
AddOrCreate<T1, T2>(Dictionary<T1, List<T2>>, T1, Func<T1, List<T2>>)
Gets or creates a list in the dictionary using a factory function.
public static List<T2> AddOrCreate<T1, T2>(Dictionary<T1, List<T2>> dictionary, T1 key, Func<T1, List<T2>> valueFactory) where T1 : notnull
Parameters
dictionaryDictionary<T1, List<T2>>The dictionary to query or modify.
keyT1The key to look up or add.
valueFactoryFunc<T1, List<T2>>Function that creates a new list from the key if it doesn't exist.
Returns
- List<T2>
The existing list or the newly created list.
Type Parameters
T1The type of the dictionary key.
T2The type of values in the list.
AddOrCreate<T, U>(Dictionary<T, List<U>>, T, U)
Adds a value to a list in the dictionary, creating the list if the key doesn't exist.
public static void AddOrCreate<T, U>(Dictionary<T, List<U>> dictionary, T key, U value) where T : notnull
Parameters
dictionaryDictionary<T, List<U>>The dictionary to modify.
keyTThe key to add or update.
valueUThe value to add to the list.
Type Parameters
TThe type of the dictionary key.
UThe type of values in the list.
AddOrCreate<Key, Value>(IDictionary<Key, List<Value>>, Key, List<Value>, bool, Dictionary<Key, List<string>>?)
Adds multiple values to a list in the dictionary, creating the list if needed.
public static void AddOrCreate<Key, Value>(IDictionary<Key, List<Value>> dictionary, Key key, List<Value> values, bool isPreventingDuplicities = false, Dictionary<Key, List<string>>? stringDictionary = null) where Key : notnull
Parameters
dictionaryIDictionary<Key, List<Value>>The dictionary to modify.
keyKeyThe key to add or update.
valuesList<Value>The values to add to the list.
isPreventingDuplicitiesboolWhether to prevent duplicate values from being added.
stringDictionaryDictionary<Key, List<string>>Optional dictionary for string comparison when preventing duplicates.
Type Parameters
KeyThe type of the dictionary key.
ValueThe type of values in the lists.
AddOrCreate<Key, Value>(IDictionary<Key, List<Value>>, Key, Value, bool, Dictionary<Key, List<string>>?)
Adds a value to a list in the dictionary, creating the list if the key doesn't exist. If dictionary contains group with name key, adds value to this group. Otherwise creates new group in dictionary with key and value.
public static void AddOrCreate<Key, Value>(IDictionary<Key, List<Value>> dictionary, Key key, Value value, bool isPreventingDuplicities = false, Dictionary<Key, List<string>>? stringDictionary = null) where Key : notnull
Parameters
dictionaryIDictionary<Key, List<Value>>The dictionary to modify.
keyKeyThe key to add or update.
valueValueThe value to add to the list.
isPreventingDuplicitiesboolWhether to prevent duplicate values from being added.
stringDictionaryDictionary<Key, List<string>>Optional dictionary for string comparison when preventing duplicates.
Type Parameters
KeyThe type of the dictionary key.
ValueThe type of values in the list.
AddOrCreate<Key, Value, ColType>(IDictionary<Key, List<Value>>, Key, Value, bool, Dictionary<Key, List<string>>?)
Adds a value to a list in the dictionary with advanced comparison options. Supports comparing collections as keys and preventing duplicate values.
public static void AddOrCreate<Key, Value, ColType>(IDictionary<Key, List<Value>> dictionary, Key key, Value value, bool isPreventingDuplicities = false, Dictionary<Key, List<string>>? stringDictionary = null) where Key : notnull
Parameters
dictionaryIDictionary<Key, List<Value>>The dictionary to modify.
keyKeyThe key to add or update.
valueValueThe value to add to the list.
isPreventingDuplicitiesboolWhether to prevent duplicate values from being added.
stringDictionaryDictionary<Key, List<string>>Optional dictionary for string comparison when preventing duplicates.
Type Parameters
KeyThe type of the dictionary key.
ValueThe type of values in the list.
ColTypeThe element type when key is a collection (use object if key is not a collection).
AddOrGet<T1, T2>(IDictionary<T1, T2>, T1, Func<T1, T2>)
Gets the value for a key if it exists, otherwise creates it using a factory function.
public static T2 AddOrGet<T1, T2>(IDictionary<T1, T2> dictionary, T1 key, Func<T1, T2> valueFactory) where T1 : notnull
Parameters
dictionaryIDictionary<T1, T2>The dictionary to query or modify.
keyT1The key to look up or add.
valueFactoryFunc<T1, T2>Function that creates a new value from the key if it doesn't exist.
Returns
- T2
The existing value or the newly created value.
Type Parameters
T1The type of the dictionary key.
T2The type of the dictionary value.
AddOrNoSet<T1, T2>(IDictionary<T1, T2>, T1, T2)
Adds a key-value pair only if the key doesn't already exist. This method has unclear purpose and is marked as obsolete.
[Obsolete("Method purpose is unclear")]
public static void AddOrNoSet<T1, T2>(IDictionary<T1, T2> dictionary, T1 key, T2 value) where T1 : notnull
Parameters
dictionaryIDictionary<T1, T2>The dictionary to modify.
keyT1The key to add.
valueT2The value to add.
Type Parameters
T1The type of the dictionary key.
T2The type of the dictionary value.
AddOrPlus<T>(Dictionary<T, int>, T, int)
Adds a new key with initial value or increments existing value by the specified amount.
public static void AddOrPlus<T>(Dictionary<T, int> dictionary, T key, int increment) where T : notnull
Parameters
dictionaryDictionary<T, int>The dictionary to modify.
keyTThe key to add or update.
incrementintThe amount to add to the value.
Type Parameters
TThe type of the dictionary key.
AddOrPlus<T>(Dictionary<T, long>, T, long)
Adds a new key with initial value or increments existing value by the specified amount.
public static void AddOrPlus<T>(Dictionary<T, long> dictionary, T key, long increment) where T : notnull
Parameters
dictionaryDictionary<T, long>The dictionary to modify.
keyTThe key to add or update.
incrementlongThe amount to add to the value.
Type Parameters
TThe type of the dictionary key.
AddOrSet<T1, T2>(IDictionary<T1, T2>, T1, T2)
Adds a new key-value pair or sets the value if the key already exists.
public static void AddOrSet<T1, T2>(IDictionary<T1, T2> dictionary, T1 key, T2 value) where T1 : notnull
Parameters
dictionaryIDictionary<T1, T2>The dictionary to modify.
keyT1The key to add or update.
valueT2The value to set.
Type Parameters
T1The type of the dictionary key.
T2The type of the dictionary value.
AddToIndexAndReturnIncrementedInt<T>(int, Dictionary<int, T>, T)
Adds a value to the dictionary at the specified index and returns the incremented index.
public static int AddToIndexAndReturnIncrementedInt<T>(int index, Dictionary<int, T> dictionary, T value)
Parameters
indexintThe current index.
dictionaryDictionary<int, T>The dictionary to modify.
valueTThe value to add.
Returns
- int
The incremented index.
Type Parameters
TThe type of the dictionary value.
AddToIndexAndReturnIncrementedShort<T>(short, Dictionary<short, T>, T)
Adds a value to the dictionary at the specified index and returns the incremented index.
public static short AddToIndexAndReturnIncrementedShort<T>(short index, Dictionary<short, T> dictionary, T value)
Parameters
indexshortThe current index.
dictionaryDictionary<short, T>The dictionary to modify.
valueTThe value to add.
Returns
- short
The incremented index.
Type Parameters
TThe type of the dictionary value.
AddToNewDictionary<T, U>(Dictionary<T, U>, T, Dictionary<T, U>, bool)
Adds a key-value pair to a new dictionary if the key exists in the source dictionary.
public static void AddToNewDictionary<T, U>(Dictionary<T, U> dictionary, T key, Dictionary<T, U> toReplace, bool isThrowingExIfNotContains = true) where T : notnull
Parameters
dictionaryDictionary<T, U>The source dictionary to check.
keyTThe key to look for.
toReplaceDictionary<T, U>The target dictionary to add to.
isThrowingExIfNotContainsboolWhether to throw exception if key is not found in source.
Type Parameters
TThe type of the dictionary key.
UThe type of the dictionary value.
Exceptions
- Exception
Thrown when key is not found and isThrowingExIfNotContains is true.
AggregateValues<T2>(Dictionary<T2, List<T2>>)
Aggregates all values from the dictionary into a single list.
public static List<T2> AggregateValues<T2>(Dictionary<T2, List<T2>> dictionary) where T2 : notnull
Parameters
dictionaryDictionary<T2, List<T2>>The dictionary containing lists of values.
Returns
- List<T2>
A single list containing all values from all entries.
Type Parameters
T2The type of values in the dictionary.
AppendLineOrCreate<T>(Dictionary<T, StringBuilder>, T, string)
Appends a line to a StringBuilder in the dictionary, creating it if it doesn't exist.
public static void AppendLineOrCreate<T>(Dictionary<T, StringBuilder> dictionary, T key, string text) where T : notnull
Parameters
dictionaryDictionary<T, StringBuilder>The dictionary to modify.
keyTThe key to add or update.
textstringThe text to append.
Type Parameters
TThe type of the dictionary key.
CalculateMedianAverageFloat(Dictionary<string, List<float>>, object)
Calculates median and average for float values in a dictionary.
public static string CalculateMedianAverageFloat(Dictionary<string, List<float>> dictionary, object textOutputGenerator)
Parameters
dictionaryDictionary<string, List<float>>The dictionary containing float lists.
textOutputGeneratorobjectThe text output generator for formatting results.
Returns
- string
A formatted string with median and average values.
Exceptions
- Exception
Thrown when dependencies (MedianAverage methods) are missing.
CategoryParser(List<string>, bool)
Parses a list into categories and their entries. Lines ending with ':' become category names, following lines become entries for that category.
public static Dictionary<string, List<string>> CategoryParser(List<string> list, bool isRemovingWhichHaveNoEntries)
Parameters
listList<string>The list of strings to parse.
isRemovingWhichHaveNoEntriesboolWhether to remove categories with "No entries" text.
Returns
- Dictionary<string, List<string>>
A dictionary mapping category names to their entry lists.
ChangeTypeOfKey<TKey, T1>(Dictionary<short, T1>)
Changes the type of dictionary keys from short to the specified type.
public static Dictionary<TKey, T1> ChangeTypeOfKey<TKey, T1>(Dictionary<short, T1> dictionary) where TKey : notnull
Parameters
dictionaryDictionary<short, T1>The dictionary with short keys.
Returns
- Dictionary<TKey, T1>
A new dictionary with converted key types.
Type Parameters
TKeyThe target key type.
T1The value type.
ChangeTypeOfKey<TKey, T1>(Dictionary<int, T1>)
Changes the type of dictionary keys from int to the specified type.
public static Dictionary<TKey, T1> ChangeTypeOfKey<TKey, T1>(Dictionary<int, T1> dictionary) where TKey : notnull
Parameters
dictionaryDictionary<int, T1>The dictionary with int keys.
Returns
- Dictionary<TKey, T1>
A new dictionary with converted key types.
Type Parameters
TKeyThe target key type.
T1The value type.
CloneDictionary<T1, T2>(Dictionary<T1, T2>)
Creates a shallow copy of a dictionary.
public static Dictionary<T1, T2> CloneDictionary<T1, T2>(Dictionary<T1, T2> dictionary) where T1 : notnull
Parameters
dictionaryDictionary<T1, T2>The dictionary to clone.
Returns
- Dictionary<T1, T2>
A new dictionary with the same key-value pairs.
Type Parameters
T1The type of the dictionary key.
T2The type of the dictionary value.
CopyTo<T, U>(Dictionary<T, U>, int)
Copies elements from a dictionary starting at a specified index.
public static void CopyTo<T, U>(Dictionary<T, U> dictionary, int arrayIndex) where T : notnull
Parameters
dictionaryDictionary<T, U>The dictionary to copy from.
arrayIndexintThe starting index to copy from.
Type Parameters
TThe type of the dictionary key.
UThe type of the dictionary value.
CopyTo<T, U>(List<KeyValuePair<T, U>>, int)
Copies elements from a list of key-value pairs starting at a specified index.
public static void CopyTo<T, U>(List<KeyValuePair<T, U>> list, int arrayIndex)
Parameters
listList<KeyValuePair<T, U>>The list to copy from.
arrayIndexintThe starting index to copy from.
Type Parameters
TThe type of the key.
UThe type of the value.
CountAllValues<Key, Value>(Dictionary<Key, List<Value>>)
Counts total number of values across all dictionary entries.
public static int CountAllValues<Key, Value>(Dictionary<Key, List<Value>> dictionary) where Key : notnull
Parameters
dictionaryDictionary<Key, List<Value>>The dictionary to count.
Returns
- int
The total count of all values in all lists.
Type Parameters
KeyThe type of the dictionary key.
ValueThe type of the values in the lists.
CountOfItems<T>(List<T>)
Counts occurrences of each item in the list.
public static List<KeyValuePair<T, int>> CountOfItems<T>(List<T> items) where T : notnull
Parameters
itemsList<T>The list of items to count.
Returns
- List<KeyValuePair<T, int>>
A list of key-value pairs sorted by count in descending order.
Type Parameters
TThe type of items in the list.
CreateTree(Dictionary<string, List<string>>)
Creates a tree structure from the dictionary.
public static object CreateTree(Dictionary<string, List<string>> dictionary)
Parameters
dictionaryDictionary<string, List<string>>The dictionary to convert to tree.
Returns
- object
A tree structure.
Exceptions
- Exception
Thrown when NTreeDictionary dependency is missing.
DivideAfter<Key, Value>(Dictionary<Key, Value>, int)
Divides a dictionary into chunks of specified size.
public static List<Dictionary<Key, Value>> DivideAfter<Key, Value>(Dictionary<Key, Value> dictionary, int chunkSize) where Key : notnull
Parameters
dictionaryDictionary<Key, Value>The dictionary to divide.
chunkSizeintThe maximum number of entries per chunk.
Returns
- List<Dictionary<Key, Value>>
A list of dictionaries, each containing up to chunkSize entries.
Type Parameters
KeyThe type of the dictionary key.
ValueThe type of the dictionary value.
GetDictionaryByKeyValueInString(string, params string[])
Creates a dictionary from a string by splitting it with delimiters.
public static Dictionary<string, string> GetDictionaryByKeyValueInString(string text, params string[] delimiters)
Parameters
textstringThe text to parse into key-value pairs.
delimitersstring[]The delimiters to split the text.
Returns
- Dictionary<string, string>
A dictionary created from alternating key-value pairs in the split text.
GetDictionaryByKeyValueInString<T>(List<T>)
Creates a dictionary from a list where alternating elements become key-value pairs.
public static Dictionary<T, T> GetDictionaryByKeyValueInString<T>(List<T> list) where T : notnull
Parameters
listList<T>The list to convert (must have even number of elements).
Returns
- Dictionary<T, T>
A dictionary created from alternating key-value pairs.
Type Parameters
TThe type of elements in the list.
Exceptions
- Exception
Thrown when the list has odd number of elements.
GetDictionaryFromIList<T1, T2>(List<KeyValuePair<T1, T2>>, bool)
Converts a list of key-value pairs to a dictionary.
public static Dictionary<T1, T2> GetDictionaryFromIList<T1, T2>(List<KeyValuePair<T1, T2>> list, bool isAddingRandomWhenKeyExists = false) where T1 : notnull
Parameters
listList<KeyValuePair<T1, T2>>The list of key-value pairs.
isAddingRandomWhenKeyExistsboolWhether to add random suffix when duplicate keys are found.
Returns
- Dictionary<T1, T2>
A dictionary containing the key-value pairs.
Type Parameters
T1The type of the key.
T2The type of the value.
GetDictionaryFromIOrderedEnumerable<T1, T2>(IOrderedEnumerable<KeyValuePair<T1, T2>>)
Converts an ordered enumerable of key-value pairs to a dictionary.
public static Dictionary<T1, T2> GetDictionaryFromIOrderedEnumerable<T1, T2>(IOrderedEnumerable<KeyValuePair<T1, T2>> orderedEnumerable) where T1 : notnull
Parameters
orderedEnumerableIOrderedEnumerable<KeyValuePair<T1, T2>>The ordered enumerable to convert.
Returns
- Dictionary<T1, T2>
A dictionary containing the key-value pairs.
Type Parameters
T1The type of the key.
T2The type of the value.
GetDictionaryFromTwoList<T1, T2>(List<T1>, List<T2>, bool)
Creates a dictionary from two lists of equal length.
public static Dictionary<T1, T2> GetDictionaryFromTwoList<T1, T2>(List<T1> firstList, List<T2> secondList, bool isAddingRandomWhenKeyExists = false) where T1 : notnull
Parameters
firstListList<T1>The list of keys.
secondListList<T2>The list of values.
isAddingRandomWhenKeyExistsboolWhether to add random suffix when duplicate keys are found.
Returns
- Dictionary<T1, T2>
A dictionary mapping first list elements to second list elements.
Type Parameters
T1The type of keys.
T2The type of values.
Exceptions
- Exception
Thrown when the lists have different counts.
GetDictionary<Key, Value>(List<Key>, List<Value>)
Creates a dictionary from two lists of keys and values.
public static Dictionary<Key, Value> GetDictionary<Key, Value>(List<Key> keys, List<Value> values) where Key : notnull
Parameters
Returns
- Dictionary<Key, Value>
A new dictionary with paired keys and values.
Type Parameters
KeyThe type of the keys.
ValueThe type of the values.
Exceptions
- Exception
Thrown when the lists have different counts.
GetFirstItemKey<Key, Value>(Dictionary<Key, Value>)
Gets the key of the first item in the dictionary.
public static Key? GetFirstItemKey<Key, Value>(Dictionary<Key, Value> dictionary) where Key : notnull
Parameters
dictionaryDictionary<Key, Value>The dictionary to query.
Returns
- Key
The key of the first entry, or default if dictionary is empty.
Type Parameters
KeyThe type of the dictionary key.
ValueThe type of the dictionary value.
GetFirstItemValue<Key, Value>(Dictionary<Key, Value>)
Gets the value of the first item in the dictionary.
public static Value? GetFirstItemValue<Key, Value>(Dictionary<Key, Value> dictionary) where Key : notnull
Parameters
dictionaryDictionary<Key, Value>The dictionary to query.
Returns
- Value
The value of the first entry, or default if dictionary is empty.
Type Parameters
KeyThe type of the dictionary key.
ValueThe type of the dictionary value.
GetIfExists(Dictionary<string, List<string>>, string, string, bool)
Gets values for a key if it exists, optionally adding prefix and suffix.
public static IList<string> GetIfExists(Dictionary<string, List<string>> dictionary, string prefix, string key, bool isAddingPrefixAndSuffix)
Parameters
dictionaryDictionary<string, List<string>>The dictionary to query.
prefixstringThe prefix to add to each value.
keystringThe key to look up.
isAddingPrefixAndSuffixboolWhether to add prefix and suffix to values.
Returns
GetListStringFromDictionary(Dictionary<string, string>)
Converts a dictionary to a flat list alternating between keys and values.
public static List<string> GetListStringFromDictionary(Dictionary<string, string> dictionary)
Parameters
dictionaryDictionary<string, string>The dictionary to convert.
Returns
GetListStringFromDictionaryDateTimeInt(IOrderedEnumerable<KeyValuePair<DateTime, int>>)
Extracts values from an ordered dictionary of DateTime to int entries.
public static List<string> GetListStringFromDictionaryDateTimeInt(IOrderedEnumerable<KeyValuePair<DateTime, int>> dictionary)
Parameters
dictionaryIOrderedEnumerable<KeyValuePair<DateTime, int>>The ordered dictionary to process.
Returns
GetListStringFromDictionaryIntInt(IOrderedEnumerable<KeyValuePair<int, int>>)
Extracts values from an ordered dictionary of int to int entries.
public static List<string> GetListStringFromDictionaryIntInt(IOrderedEnumerable<KeyValuePair<int, int>> dictionary)
Parameters
dictionaryIOrderedEnumerable<KeyValuePair<int, int>>The ordered dictionary to process.
Returns
GetOrKey<T>(Dictionary<T, string>, T)
Gets the value for a key, or the key itself converted to string if not found.
public static string GetOrKey<T>(Dictionary<T, string> dictionary, T key) where T : notnull
Parameters
dictionaryDictionary<T, string>The dictionary to query.
keyTThe key to look up.
Returns
- string
The value if key exists, otherwise the key as string.
Type Parameters
TThe type of the dictionary key.
GetValuesOrEmpty<T, U>(IDictionary<T, List<U>>, T)
Gets values for a key if it exists, otherwise returns empty list.
public static List<U> GetValuesOrEmpty<T, U>(IDictionary<T, List<U>> dictionary, T key) where T : notnull
Parameters
dictionaryIDictionary<T, List<U>>The dictionary to query.
keyTThe key to look up.
Returns
- List<U>
The list of values if key exists, otherwise empty list.
Type Parameters
TThe type of the dictionary key.
UThe type of values in the list.
GroupByValues<U, T>(Dictionary<U, T>)
Groups dictionary entries by their values, swapping keys and values.
public static Dictionary<T, List<U>> GroupByValues<U, T>(Dictionary<U, T> dictionary) where U : notnull where T : notnull
Parameters
dictionaryDictionary<U, T>The dictionary to group.
Returns
- Dictionary<T, List<U>>
A dictionary where original values become keys with lists of original keys as values.
Type Parameters
UThe original key type.
TThe original value type.
IncrementOrCreate<T>(Dictionary<T, int>, T)
Increments the value for a key, or creates it with value 1 if it doesn't exist.
public static void IncrementOrCreate<T>(Dictionary<T, int> dictionary, T key) where T : notnull
Parameters
dictionaryDictionary<T, int>The dictionary to modify.
keyTThe key to increment.
Type Parameters
TThe type of the dictionary key.
KeepOnlyKeys(Dictionary<string, string>, List<string>)
Keeps only specified keys in the dictionary, removing all others.
public static Dictionary<string, string> KeepOnlyKeys(Dictionary<string, string> dictionary, List<string> includeAlways)
Parameters
dictionaryDictionary<string, string>The dictionary to filter.
includeAlwaysList<string>List of keys to keep in the dictionary.
Returns
- Dictionary<string, string>
The filtered dictionary.
RemoveDuplicatedFromDictionaryByValues<T1, T2>(Dictionary<T1, T2>, Dictionary<T1, T2>?)
Removes duplicate entries from dictionary by comparing values. Entries with duplicate values are removed and optionally stored in the duplicates dictionary.
public static Dictionary<T1, T2> RemoveDuplicatedFromDictionaryByValues<T1, T2>(Dictionary<T1, T2> dictionary, Dictionary<T1, T2>? duplicates) where T1 : notnull
Parameters
dictionaryDictionary<T1, T2>The dictionary to process.
duplicatesDictionary<T1, T2>Optional dictionary to store removed duplicates (can be null).
Returns
- Dictionary<T1, T2>
The dictionary with duplicates removed.
Type Parameters
T1The type of the dictionary key.
T2The type of the dictionary value.
RemoveIfExists<T, U>(Dictionary<T, List<U>>, T)
Removes a key from the dictionary if it exists.
public static void RemoveIfExists<T, U>(Dictionary<T, List<U>> dictionary, T key) where T : notnull
Parameters
dictionaryDictionary<T, List<U>>The dictionary to modify.
keyTThe key to remove.
Type Parameters
TThe type of the dictionary key.
UThe type of the dictionary value.
ReturnsCopy<T, U>(Dictionary<T, U>)
Creates a copy of the dictionary.
public static Dictionary<T, U> ReturnsCopy<T, U>(Dictionary<T, U> dictionary) where T : notnull
Parameters
dictionaryDictionary<T, U>The dictionary to copy.
Returns
- Dictionary<T, U>
A new dictionary with the same key-value pairs.
Type Parameters
TThe type of the dictionary key.
UThe type of the dictionary value.
SwitchKeyAndValue<T, U>(Dictionary<T, U>)
Swaps keys and values in a dictionary.
public static Dictionary<U, T> SwitchKeyAndValue<T, U>(Dictionary<T, U> dictionary) where T : notnull where U : notnull
Parameters
dictionaryDictionary<T, U>The dictionary to transform.
Returns
- Dictionary<U, T>
A new dictionary with keys and values swapped.
Type Parameters
TThe original key type.
UThe original value type.