Show / Hide Table of Contents

Class Dictionary

Represents various extensions for types Dictionary<TKey, TValue> and IDictionary<TKey, TValue>.

Inheritance
object
Dictionary
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext.Collections.Generic
Assembly: DotNext.dll
Syntax
public static class Dictionary

Methods

| Edit this page View Source

ConvertValues<TKey, TValue, TResult>(IReadOnlyDictionary<TKey, TValue>, Converter<TValue, TResult>)

Applies lazy conversion for each dictionary value.

Declaration
public static ReadOnlyDictionaryView<TKey, TValue, TResult> ConvertValues<TKey, TValue, TResult>(this IReadOnlyDictionary<TKey, TValue> dictionary, Converter<TValue, TResult> mapper)
Parameters
Type Name Description
IReadOnlyDictionary<TKey, TValue> dictionary

A dictionary to be mapped.

Converter<TValue, TResult> mapper

Mapping function.

Returns
Type Description
ReadOnlyDictionaryView<TKey, TValue, TResult>

Read-only view of the dictionary where each value is converted in lazy manner.

Type Parameters
Name Description
TKey

Type of keys.

TValue

Type of values.

TResult

Type of mapped values.

| Edit this page View Source

ForEach<TKey, TValue>(IDictionary<TKey, TValue>, Action<TKey, TValue>)

Applies specific action to each dictionary.

Declaration
public static void ForEach<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue> action)
Parameters
Type Name Description
IDictionary<TKey, TValue> dictionary

A dictionary to read from.

Action<TKey, TValue> action

The action to be applied for each key/value pair.

Type Parameters
Name Description
TKey

The key type of the dictionary.

TValue

The value type of the dictionary.

| Edit this page View Source

GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey)

Adds a key-value pair to the dictionary if the key does not exist.

Declaration
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TKey : notnull where TValue : new()
Parameters
Type Name Description
Dictionary<TKey, TValue> dictionary

The source dictionary.

TKey key

The key of the key-value pair.

Returns
Type Description
TValue

The corresponding value in the dictionary if key already exists, or a new instance of TValue.

Type Parameters
Name Description
TKey

The key type of the dictionary.

TValue

The value type of the dictionary.

| Edit this page View Source

GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, Func<TKey, TValue>)

Generates a value and adds the key-value pair to the dictionary if the key does not exist.

Declaration
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory) where TKey : notnull
Parameters
Type Name Description
Dictionary<TKey, TValue> dictionary

The source dictionary.

TKey key

The key of the key-value pair.

Func<TKey, TValue> valueFactory

The function used to generate the value from the key.

Returns
Type Description
TValue

The corresponding value in the dictionary if key already exists, or the value generated by valueFactory.

Type Parameters
Name Description
TKey

The key type of the dictionary.

TValue

The value type of the dictionary.

| Edit this page View Source

GetOrAdd<TKey, TValue>(Dictionary<TKey, TValue>, TKey, TValue)

Adds a key-value pair to the dictionary if the key does not exist.

Declaration
public static TValue GetOrAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) where TKey : notnull
Parameters
Type Name Description
Dictionary<TKey, TValue> dictionary

The source dictionary.

TKey key

The key of the key-value pair.

TValue value

The value of the key-value pair.

Returns
Type Description
TValue

The corresponding value in the dictionary if key already exists, or value.

Type Parameters
Name Description
TKey

The key type of the dictionary.

TValue

The value type of the dictionary.

| Edit this page View Source

GetOrAdd<TKey, TValue, TFactory>(Dictionary<TKey, TValue>, TKey, Func<TKey, TValue>)

Generates a value and adds the key-value pair to the dictionary if the key does not exist.

Declaration
public static TValue GetOrAdd<TKey, TValue, TFactory>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory) where TKey : notnull where TFactory : struct, ISupplier<TKey, TValue>
Parameters
Type Name Description
Dictionary<TKey, TValue> dictionary

The source dictionary.

TKey key

The key of the key-value pair.

Func<TKey, TValue> valueFactory

The function used to generate the value from the key.

Returns
Type Description
TValue

The corresponding value in the dictionary if key already exists, or the value generated by valueFactory.

Type Parameters
Name Description
TKey

The key type of the dictionary.

TValue

The value type of the dictionary.

TFactory

The type of the value factory.

| Edit this page View Source

GetOrInvoke<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<TValue>)

Gets dictionary value by key if it exists or invoke defaultValue and return its result as a default value.

Declaration
public static TValue GetOrInvoke<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> defaultValue)
Parameters
Type Name Description
IDictionary<TKey, TValue> dictionary

A dictionary to read from.

TKey key

A key associated with the value.

Func<TValue> defaultValue

A delegate to be invoked if key doesn't exist in the dictionary.

Returns
Type Description
TValue

The value associated with the key or returned by the delegate.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

| Edit this page View Source

GetOrInvoke<TKey, TValue, TSupplier>(IDictionary<TKey, TValue>, TKey, TSupplier)

Gets dictionary value by key if it exists or invoke defaultValue and return its result as a default value.

Declaration
public static TValue GetOrInvoke<TKey, TValue, TSupplier>(this IDictionary<TKey, TValue> dictionary, TKey key, TSupplier defaultValue) where TSupplier : struct, ISupplier<TValue>
Parameters
Type Name Description
IDictionary<TKey, TValue> dictionary

A dictionary to read from.

TKey key

A key associated with the value.

TSupplier defaultValue

A delegate to be invoked if key doesn't exist in the dictionary.

Returns
Type Description
TValue

The value associated with the key or returned by the delegate.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

TSupplier

Type of default value supplier.

| Edit this page View Source

IndexerGetter<TKey, TValue>(IDictionary<TKey, TValue>)

Returns this[TKey].get as delegate attached to the dictionary instance.

Declaration
public static Func<TKey, TValue> IndexerGetter<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
Parameters
Type Name Description
IDictionary<TKey, TValue> dictionary

Mutable dictionary instance.

Returns
Type Description
Func<TKey, TValue>

A delegate representing dictionary indexer.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

| Edit this page View Source

IndexerGetter<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>)

Returns this[TKey].get as delegate attached to the dictionary instance.

Declaration
public static Func<TKey, TValue> IndexerGetter<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary)
Parameters
Type Name Description
IReadOnlyDictionary<TKey, TValue> dictionary

Read-only dictionary instance.

Returns
Type Description
Func<TKey, TValue>

A delegate representing dictionary indexer.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

| Edit this page View Source

IndexerSetter<TKey, TValue>(IDictionary<TKey, TValue>)

Returns this[TKey].set as delegate attached to the dictionary instance.

Declaration
public static Action<TKey, TValue> IndexerSetter<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
Parameters
Type Name Description
IDictionary<TKey, TValue> dictionary

Mutable dictionary instance.

Returns
Type Description
Action<TKey, TValue>

A delegate representing dictionary indexer.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

| Edit this page View Source

KeysGetter<TKey, TValue>(IDictionary<TKey, TValue>)

Returns Keys as delegate attached to the dictionary instance.

Declaration
public static Func<ICollection<TKey>> KeysGetter<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
Parameters
Type Name Description
IDictionary<TKey, TValue> dictionary

Read-only dictionary instance.

Returns
Type Description
Func<ICollection<TKey>>

A delegate providing access to dictionary keys.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

| Edit this page View Source

KeysGetter<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>)

Returns Keys as delegate attached to the dictionary instance.

Declaration
public static Func<IEnumerable<TKey>> KeysGetter<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary)
Parameters
Type Name Description
IReadOnlyDictionary<TKey, TValue> dictionary

Read-only dictionary instance.

Returns
Type Description
Func<IEnumerable<TKey>>

A delegate providing access to dictionary keys.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

| Edit this page View Source

TryGetValue<TKey, TValue>(IDictionary<TKey, TValue>, TKey)

Gets the value associated with the specified key.

Declaration
public static Optional<TValue> TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
Parameters
Type Name Description
IDictionary<TKey, TValue> dictionary

A dictionary to read from.

TKey key

The key whose value to get.

Returns
Type Description
Optional<TValue>

The optional value associated with the key.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

| Edit this page View Source

TryGetValue<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>, TKey)

Gets the value associated with the specified key.

Declaration
public static Optional<TValue> TryGetValue<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> dictionary, TKey key)
Parameters
Type Name Description
IReadOnlyDictionary<TKey, TValue> dictionary

A dictionary to read from.

TKey key

The key whose value to get.

Returns
Type Description
Optional<TValue>

The optional value associated with the key.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

| Edit this page View Source

TryRemove<TKey, TValue>(IDictionary<TKey, TValue>, TKey)

Removes the value with the specified key and return the removed value.

Declaration
public static Optional<TValue> TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
Parameters
Type Name Description
IDictionary<TKey, TValue> dictionary

A dictionary to modify.

TKey key

The key of the element to remove.

Returns
Type Description
Optional<TValue>

The removed value.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

| Edit this page View Source

ValuesGetter<TKey, TValue>(IDictionary<TKey, TValue>)

Returns Values as delegate attached to the dictionary instance.

Declaration
public static Func<ICollection<TValue>> ValuesGetter<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
Parameters
Type Name Description
IDictionary<TKey, TValue> dictionary

Read-only dictionary instance.

Returns
Type Description
Func<ICollection<TValue>>

A delegate providing access to dictionary keys.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

| Edit this page View Source

ValuesGetter<TKey, TValue>(IReadOnlyDictionary<TKey, TValue>)

Returns Values as delegate attached to the dictionary instance.

Declaration
public static Func<IEnumerable<TValue>> ValuesGetter<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary)
Parameters
Type Name Description
IReadOnlyDictionary<TKey, TValue> dictionary

Read-only dictionary instance.

Returns
Type Description
Func<IEnumerable<TValue>>

A delegate providing access to dictionary keys.

Type Parameters
Name Description
TKey

Type of dictionary keys.

TValue

Type of dictionary values.

  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾