DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.1.1" />

 Dictionary

public static class Dictionary
Represents various extensions for types Dictionary<T, U> and IDictionary<T, U>.
using System; using System.Collections.Generic; using System.Reflection; using System.Runtime.CompilerServices; namespace DotNext.Collections.Generic { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class Dictionary { [System.Runtime.CompilerServices.Nullable(0)] public static class Indexer<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue> { public static Func<IReadOnlyDictionary<TKey, TValue>, TKey, TValue> ReadOnly { get; } public static Func<IDictionary<TKey, TValue>, TKey, TValue> Getter { get; } public static Action<IDictionary<TKey, TValue>, TKey, TValue> Setter { get; } static Indexer() { RuntimeMethodHandle handle = (RuntimeMethodHandle); RuntimeTypeHandle declaringType = typeof(IReadOnlyDictionary<TKey, TValue>).TypeHandle; ReadOnly = ((MethodInfo)MethodBase.GetMethodFromHandle(handle, declaringType)).CreateDelegate<Func<IReadOnlyDictionary<TKey, TValue>, TKey, TValue>>(); handle = (RuntimeMethodHandle); declaringType = typeof(IDictionary<TKey, TValue>).TypeHandle; Getter = ((MethodInfo)MethodBase.GetMethodFromHandle(handle, declaringType)).CreateDelegate<Func<IDictionary<TKey, TValue>, TKey, TValue>>(); handle = (RuntimeMethodHandle); Setter = ((MethodInfo)MethodBase.GetMethodFromHandle(handle, declaringType)).CreateDelegate<Action<IDictionary<TKey, TValue>, TKey, TValue>>(); } } public static Func<TKey, TValue> IndexerGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary) { return dictionary.get_Item; } public static Func<IEnumerable<TKey>> KeysGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary) { return dictionary.get_Keys; } public static Func<IEnumerable<TValue>> ValuesGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary) { return (Func<IEnumerable<TValue>>)new Func<IEnumerable<TKey>>(dictionary.get_Values); } public static Func<TKey, TValue> IndexerGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary) { return dictionary.get_Item; } public static Action<TKey, TValue> IndexerSetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary) { return dictionary.set_Item; } public static Func<ICollection<TKey>> KeysGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary) { return dictionary.get_Keys; } public static Func<ICollection<TValue>> ValuesGetter<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary) { return (Func<ICollection<TValue>>)new Func<ICollection<TKey>>(dictionary.get_Values); } public static TValue GetOrAdd<TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this Dictionary<TKey, TValue> dictionary, TKey key) where TValue : new { if (!dictionary.TryGetValue(key, out TValue value)) dictionary.Add(key, value = new TValue()); return value; } public static TValue GetOrAdd<TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value) { if (dictionary.TryGetValue(key, out TValue value2)) value = value2; else dictionary.Add(key, value); return value; } public static TValue GetOrAdd<TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue, [System.Runtime.CompilerServices.Nullable(0)] TFactory>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory) where TFactory : struct, ISupplier<TKey, TValue> { if (!dictionary.TryGetValue(key, out TValue value)) { value = valueFactory(key); dictionary.Add(key, value); } return value; } public static TValue GetOrAdd<TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, Func<TKey, TValue> valueFactory) { return dictionary.GetOrAdd<TKey, TValue, DelegatingSupplier<TKey, TValue>>(key, valueFactory); } public static void ForEach<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary, Action<TKey, TValue> action) { foreach (KeyValuePair<TKey, TValue> item in (IEnumerable<KeyValuePair<TKey, TValue>>)dictionary) { item.Deconstruct(out TKey key, out TValue value); TKey arg = key; TValue arg2 = value; action(arg, arg2); } } public static TValue GetOrInvoke<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue, [System.Runtime.CompilerServices.Nullable(0)] TSupplier>(this IDictionary<TKey, TValue> dictionary, TKey key, [System.Runtime.CompilerServices.Nullable(0)] TSupplier defaultValue) where TSupplier : struct, ISupplier<TValue> { if (!dictionary.TryGetValue(key, out TValue value)) return ((ISupplier<TValue>)defaultValue).Invoke(); return value; } public static TValue GetOrInvoke<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> defaultValue) { return dictionary.GetOrInvoke(key, (DelegatingSupplier<TValue>)defaultValue); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<TValue> TryGetValue<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) { if (!dictionary.TryGetValue(key, out TValue value)) return Optional<TValue>.None; return new Optional<TValue>(value); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<TValue> TryRemove<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) { if (!dictionary.TryGetValue(key, out TValue value) || !dictionary.Remove(key)) return Optional<TValue>.None; return new Optional<TValue>(value); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Optional<TValue> TryGetValue<[System.Runtime.CompilerServices.Nullable(2)] TKey, [System.Runtime.CompilerServices.Nullable(2)] TValue>(IReadOnlyDictionary<TKey, TValue> dictionary, TKey key) { if (!dictionary.TryGetValue(key, out TValue value)) return Optional<TValue>.None; return new Optional<TValue>(value); } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1, 1, 1 })] public static ReadOnlyDictionaryView<TKey, TValue, TResult> ConvertValues<TKey, TValue, TResult>([System.Runtime.CompilerServices.Nullable(1)] this IReadOnlyDictionary<TKey, TValue> dictionary, [System.Runtime.CompilerServices.Nullable(1)] Converter<TValue, TResult> mapper) { return new ReadOnlyDictionaryView<TKey, TValue, TResult>(dictionary, mapper); } } }