Table of Contents

DictionaryExtensions Class

Definition

Namespace
Cuemon.Extensions.Collections.Generic
Assemblies
Cuemon.Extensions.Collections.Generic.dll
Source
src/Cuemon.Extensions.Collections.Generic/DictionaryExtensions.cs

Extension methods for the IDictionary<TKey, TValue> interface.

public static class DictionaryExtensions
Inheritance
DictionaryExtensions

Examples

DictionaryExtensions provides extension methods for IDictionary<TKey, TValue> including CopyTo, TryAdd with a predicate, AddOrUpdate, GetValueOrDefault with a factory fallback, and TryGetValueOrFallback. This example creates a source dictionary with "alpha": 1 and "beta": 2, copies entries to a new dictionary via CopyTo, conditionally adds "gamma": 3 only if the key does not exist, updates "beta" to 20, retrieves "delta" with a fallback factory returning 42, and resolves a missing key using TryGetValueOrFallback. Console output confirms the destination count, fallback values, and the formatted key-value pairs.

using System;
using System.Collections.Generic;
using System.Linq;
using Cuemon.Extensions.Collections.Generic;

namespace MyApp.Examples
{
    public static class DictionaryExtensionsExample
    {
        public static void Demonstrate()
        {
            IDictionary<string, int> source = new Dictionary<string, int>
            {
                ["alpha"] = 1,
                ["beta"] = 2
            };

            var destination = new Dictionary<string, int>();
            source.CopyTo(destination);

            source.TryAdd("gamma", 3, dictionary => !dictionary.ContainsKey("gamma"));
            source.AddOrUpdate("beta", 20);

            var configuredFallback = source.GetValueOrDefault("delta", () => 42);
            var foundFallback = source.TryGetValueOrFallback("missing", keys => keys.OrderBy(key => key).First(), out var fallbackValue);
            var rows = source.ToEnumerable().Select(pair => $"{pair.Key}:{pair.Value}");

            Console.WriteLine(destination.Count);
            Console.WriteLine(configuredFallback);
            Console.WriteLine(foundFallback);
            Console.WriteLine(fallbackValue);
            Console.WriteLine(string.Join(", ", rows));
        }
    }
}

Methods

Name Description
AddOrUpdate<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue)

Attempts to add or update an existing element with the provided key to the dictionary with the specified value.

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

Copies all elements from source to destination.

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

Copies elements from source to destination using the copier delegate.

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

Gets the value associated with the specified key or a default value through defaultProvider when the key does not exists in the dictionary.

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

Returns the specified dictionary typed as KeyValuePair<TKey, TValue> sequence.

TryAdd<TKey, TValue>(IDictionary<TKey, TValue>, TKey, TValue, Func<IDictionary<TKey, TValue>, bool>)

Attempts to add the specified key and value to the dictionary.

TryGetValueOrFallback<TKey, TValue>(IDictionary<TKey, TValue>, TKey, Func<IEnumerable<TKey>, TKey>, out TValue)

Gets the value associated with the specified key.