Table of Contents

DictionaryDecoratorExtensions Class

Definition

Extension methods for the IDictionary<TKey, TValue> interface hidden behind the IDecorator<T> interface.

public static class DictionaryDecoratorExtensions
Inheritance
DictionaryDecoratorExtensions

Examples

The following example demonstrates dictionary extension methods available through the Decorator wrapper. It shows get-or-fallback, try-add, add-or-update, copying, and depth-indexing operations, printing each result to the console.

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

namespace Contoso.Inventory;

public sealed class DictionaryDecoratorExtensionsExample
{
    public static void Run()
    {
        IDictionary<int, string> catalog = new Dictionary<int, string>
        {
            [1] = "apples",
            [2] = "bananas"
        };

        var decorated = Decorator.Enclose(catalog);
        string fallback = decorated.GetValueOrDefault(3, () => "not found");
        bool found = decorated.TryGetValueOrFallback(42, keys => 2, out var alias);
        bool added = decorated.TryAdd(3, "cherries");
        decorated.AddOrUpdate(2, "blueberries");
        KeyValuePair<int, string>[] entries = decorated.ToEnumerable().ToArray();

        IDictionary<int, string> copy = decorated.CopyTo(new Dictionary<int, string>());

        IDictionary<int, Dictionary<int, int>> depthIndexes = new Dictionary<int, Dictionary<int, int>>();
        int depthIndex = Decorator.Enclose(depthIndexes).GetDepthIndex(readerDepth: 0, index: 1, nesting: 0);

        Console.WriteLine($"Fallback: {fallback}");
        Console.WriteLine($"Found fallback key: {found} -> {alias}");
        Console.WriteLine($"Added key 3: {added}");
        Console.WriteLine($"Enumerated entries: {entries.Length}");
        Console.WriteLine($"Copied entries: {copy.Count}");
        Console.WriteLine($"Depth index: {depthIndex}");
    }
}

Methods

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

Attempts to add or update an existing element with the provided key to the enclosed IDictionary<TKey, TValue> of the decorator with the specified value.

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

Copies all elements from the enclosed IDictionary<TKey, TValue> of decorator to destination.

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

Copies elements from the enclosed IDictionary<TKey, TValue> of decorator to destination using the copier delegate.

GetDepthIndex(IDecorator<IDictionary<int, Dictionary<int, int>>>, int, int, int)

Gets the level of nesting of the enclosed IDictionary<TKey, TValue> of the decorator. This API supports the product infrastructure and is not intended to be used directly from your code.

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

Gets the value associated with the specified key or default when the key does not exists in the enclosed IDictionary<TKey, TValue> of the decorator.

GetValueOrDefault<TKey, TValue>(IDecorator<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 enclosed IDictionary<TKey, TValue> of the decorator.

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

Returns the enclosed IDictionary<TKey, TValue> of the decorator typed as KeyValuePair<TKey, TValue> sequence.

TryAdd<TKey, TValue>(IDecorator<IDictionary<TKey, TValue>>, TKey, TValue)

Attempts to add the specified key and value to the enclosed IDictionary<TKey, TValue> of the decorator.

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

Attempts to add the specified key and value to the enclosed IDictionary<TKey, TValue> of the decorator.

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

Gets the value associated with the specified key from the enclosed IDictionary<TKey, TValue> of the decorator.

See Also