Table of Contents

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

dictionary Dictionary<Key, List<Value>>

The dictionary to modify.

key Key

The key to add or update.

value Value

The value to add to the list.

Type Parameters

Key

The type of the dictionary key.

Value

The 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

dictionary Dictionary<Key, TimeSpan>

The dictionary to modify.

key Key

The key to add or update.

value DateTime

The DateTime value to convert to TimeSpan and add.

Type Parameters

Key

The 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

dictionary Dictionary<Key, TimeSpan>

The dictionary to modify.

key Key

The key to add or update.

value TimeSpan

The TimeSpan value to add.

Type Parameters

Key

The 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

dictionary Dictionary<T1, List<T2>>

The dictionary to query or modify.

key T1

The key to look up or add.

valueFactory Func<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

T1

The type of the dictionary key.

T2

The 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

dictionary Dictionary<T, List<U>>

The dictionary to modify.

key T

The key to add or update.

value U

The value to add to the list.

Type Parameters

T

The type of the dictionary key.

U

The 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

dictionary IDictionary<Key, List<Value>>

The dictionary to modify.

key Key

The key to add or update.

values List<Value>

The values to add to the list.

isPreventingDuplicities bool

Whether to prevent duplicate values from being added.

stringDictionary Dictionary<Key, List<string>>

Optional dictionary for string comparison when preventing duplicates.

Type Parameters

Key

The type of the dictionary key.

Value

The 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

dictionary IDictionary<Key, List<Value>>

The dictionary to modify.

key Key

The key to add or update.

value Value

The value to add to the list.

isPreventingDuplicities bool

Whether to prevent duplicate values from being added.

stringDictionary Dictionary<Key, List<string>>

Optional dictionary for string comparison when preventing duplicates.

Type Parameters

Key

The type of the dictionary key.

Value

The 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

dictionary IDictionary<Key, List<Value>>

The dictionary to modify.

key Key

The key to add or update.

value Value

The value to add to the list.

isPreventingDuplicities bool

Whether to prevent duplicate values from being added.

stringDictionary Dictionary<Key, List<string>>

Optional dictionary for string comparison when preventing duplicates.

Type Parameters

Key

The type of the dictionary key.

Value

The type of values in the list.

ColType

The 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

dictionary IDictionary<T1, T2>

The dictionary to query or modify.

key T1

The key to look up or add.

valueFactory Func<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

T1

The type of the dictionary key.

T2

The 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

dictionary IDictionary<T1, T2>

The dictionary to modify.

key T1

The key to add.

value T2

The value to add.

Type Parameters

T1

The type of the dictionary key.

T2

The 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

dictionary Dictionary<T, int>

The dictionary to modify.

key T

The key to add or update.

increment int

The amount to add to the value.

Type Parameters

T

The 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

dictionary Dictionary<T, long>

The dictionary to modify.

key T

The key to add or update.

increment long

The amount to add to the value.

Type Parameters

T

The 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

dictionary IDictionary<T1, T2>

The dictionary to modify.

key T1

The key to add or update.

value T2

The value to set.

Type Parameters

T1

The type of the dictionary key.

T2

The 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

index int

The current index.

dictionary Dictionary<int, T>

The dictionary to modify.

value T

The value to add.

Returns

int

The incremented index.

Type Parameters

T

The 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

index short

The current index.

dictionary Dictionary<short, T>

The dictionary to modify.

value T

The value to add.

Returns

short

The incremented index.

Type Parameters

T

The 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

dictionary Dictionary<T, U>

The source dictionary to check.

key T

The key to look for.

toReplace Dictionary<T, U>

The target dictionary to add to.

isThrowingExIfNotContains bool

Whether to throw exception if key is not found in source.

Type Parameters

T

The type of the dictionary key.

U

The 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

dictionary Dictionary<T2, List<T2>>

The dictionary containing lists of values.

Returns

List<T2>

A single list containing all values from all entries.

Type Parameters

T2

The 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

dictionary Dictionary<T, StringBuilder>

The dictionary to modify.

key T

The key to add or update.

text string

The text to append.

Type Parameters

T

The 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

dictionary Dictionary<string, List<float>>

The dictionary containing float lists.

textOutputGenerator object

The 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

list List<string>

The list of strings to parse.

isRemovingWhichHaveNoEntries bool

Whether 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

dictionary Dictionary<short, T1>

The dictionary with short keys.

Returns

Dictionary<TKey, T1>

A new dictionary with converted key types.

Type Parameters

TKey

The target key type.

T1

The 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

dictionary Dictionary<int, T1>

The dictionary with int keys.

Returns

Dictionary<TKey, T1>

A new dictionary with converted key types.

Type Parameters

TKey

The target key type.

T1

The 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

dictionary Dictionary<T1, T2>

The dictionary to clone.

Returns

Dictionary<T1, T2>

A new dictionary with the same key-value pairs.

Type Parameters

T1

The type of the dictionary key.

T2

The 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

dictionary Dictionary<T, U>

The dictionary to copy from.

arrayIndex int

The starting index to copy from.

Type Parameters

T

The type of the dictionary key.

U

The 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

list List<KeyValuePair<T, U>>

The list to copy from.

arrayIndex int

The starting index to copy from.

Type Parameters

T

The type of the key.

U

The 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

dictionary Dictionary<Key, List<Value>>

The dictionary to count.

Returns

int

The total count of all values in all lists.

Type Parameters

Key

The type of the dictionary key.

Value

The 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

items List<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

T

The 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

dictionary Dictionary<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

dictionary Dictionary<Key, Value>

The dictionary to divide.

chunkSize int

The maximum number of entries per chunk.

Returns

List<Dictionary<Key, Value>>

A list of dictionaries, each containing up to chunkSize entries.

Type Parameters

Key

The type of the dictionary key.

Value

The 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

text string

The text to parse into key-value pairs.

delimiters string[]

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

list List<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

T

The 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

list List<KeyValuePair<T1, T2>>

The list of key-value pairs.

isAddingRandomWhenKeyExists bool

Whether to add random suffix when duplicate keys are found.

Returns

Dictionary<T1, T2>

A dictionary containing the key-value pairs.

Type Parameters

T1

The type of the key.

T2

The 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

orderedEnumerable IOrderedEnumerable<KeyValuePair<T1, T2>>

The ordered enumerable to convert.

Returns

Dictionary<T1, T2>

A dictionary containing the key-value pairs.

Type Parameters

T1

The type of the key.

T2

The 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

firstList List<T1>

The list of keys.

secondList List<T2>

The list of values.

isAddingRandomWhenKeyExists bool

Whether 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

T1

The type of keys.

T2

The 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

keys List<Key>

The list of keys.

values List<Value>

The list of values.

Returns

Dictionary<Key, Value>

A new dictionary with paired keys and values.

Type Parameters

Key

The type of the keys.

Value

The 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

dictionary Dictionary<Key, Value>

The dictionary to query.

Returns

Key

The key of the first entry, or default if dictionary is empty.

Type Parameters

Key

The type of the dictionary key.

Value

The 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

dictionary Dictionary<Key, Value>

The dictionary to query.

Returns

Value

The value of the first entry, or default if dictionary is empty.

Type Parameters

Key

The type of the dictionary key.

Value

The 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

dictionary Dictionary<string, List<string>>

The dictionary to query.

prefix string

The prefix to add to each value.

key string

The key to look up.

isAddingPrefixAndSuffix bool

Whether to add prefix and suffix to values.

Returns

IList<string>

The list of values if key exists, otherwise empty list.

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

dictionary Dictionary<string, string>

The dictionary to convert.

Returns

List<string>

A list with alternating key-value entries.

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

dictionary IOrderedEnumerable<KeyValuePair<DateTime, int>>

The ordered dictionary to process.

Returns

List<string>

A list of string representations of the values.

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

dictionary IOrderedEnumerable<KeyValuePair<int, int>>

The ordered dictionary to process.

Returns

List<string>

A list of string representations of the values.

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

dictionary Dictionary<T, string>

The dictionary to query.

key T

The key to look up.

Returns

string

The value if key exists, otherwise the key as string.

Type Parameters

T

The 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

dictionary IDictionary<T, List<U>>

The dictionary to query.

key T

The key to look up.

Returns

List<U>

The list of values if key exists, otherwise empty list.

Type Parameters

T

The type of the dictionary key.

U

The 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

dictionary Dictionary<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

U

The original key type.

T

The 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

dictionary Dictionary<T, int>

The dictionary to modify.

key T

The key to increment.

Type Parameters

T

The 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

dictionary Dictionary<string, string>

The dictionary to filter.

includeAlways List<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

dictionary Dictionary<T1, T2>

The dictionary to process.

duplicates Dictionary<T1, T2>

Optional dictionary to store removed duplicates (can be null).

Returns

Dictionary<T1, T2>

The dictionary with duplicates removed.

Type Parameters

T1

The type of the dictionary key.

T2

The 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

dictionary Dictionary<T, List<U>>

The dictionary to modify.

key T

The key to remove.

Type Parameters

T

The type of the dictionary key.

U

The 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

dictionary Dictionary<T, U>

The dictionary to copy.

Returns

Dictionary<T, U>

A new dictionary with the same key-value pairs.

Type Parameters

T

The type of the dictionary key.

U

The 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

dictionary Dictionary<T, U>

The dictionary to transform.

Returns

Dictionary<U, T>

A new dictionary with keys and values swapped.

Type Parameters

T

The original key type.

U

The original value type.