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.
// Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using Cuemon;
using Cuemon.Collections.Generic;
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}");
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.
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.
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.