< Summary

Information
Class: MyNet.Collections.DictionaryExtensions
Assembly: MyNet.Collections
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Collections/Extensions/DictionaryExtensions.cs
Tag: 342_31701152804
Line coverage
100%
Covered lines: 15
Uncovered lines: 0
Coverable lines: 15
Total lines: 76
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetOrCreate(...)100%22100%
GetOrDefault(...)100%22100%
Merge(...)100%11100%
Merge(...)100%44100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Collections/Extensions/DictionaryExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="DictionaryExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9
 10#pragma warning disable IDE0130 // Namespace does not match folder structure
 11namespace MyNet.Collections;
 12#pragma warning restore IDE0130 // Namespace does not match folder structure
 13
 14/// <summary>
 15/// Extension methods for dictionary-like collections.
 16/// </summary>
 17public static class DictionaryExtensions
 18{
 19    extension<TKey, TValue>(IDictionary<TKey, TValue> dictionary)
 20        where TKey : notnull
 21    {
 22        /// <summary>
 23        /// Gets the value for the specified key or adds the provided default value if missing.
 24        /// </summary>
 25        public TValue GetOrCreate(TKey key, Func<TValue> factory)
 26        {
 627            ArgumentNullException.ThrowIfNull(dictionary);
 28
 929            if (dictionary.TryGetValue(key, out var value)) return value;
 330            value = factory();
 331            dictionary.Add(key, value);
 32
 333            return value;
 34        }
 35
 36        /// <summary>
 37        /// Gets the value for the specified key or returns a default when missing.
 38        /// </summary>
 39        public TValue? GetOrDefault(TKey key, TValue? defaultValue = default)
 40        {
 941            ArgumentNullException.ThrowIfNull(dictionary);
 42
 943            return dictionary.TryGetValue(key, out var value) ? value : defaultValue;
 44        }
 45
 46        /// <summary>
 47        /// Merges two dictionaries into a new dictionary. Keys are expected to be unique.
 48        /// </summary>
 49        public Dictionary<TKey, TValue> Merge(IDictionary<TKey, TValue> dictionary2)
 50        {
 651            ArgumentNullException.ThrowIfNull(dictionary);
 52
 653            return new[] { dictionary, dictionary2 }.Merge();
 54        }
 55    }
 56
 57    /// <summary>
 58    /// Merges multiple dictionaries into a new dictionary. Keys are expected to be unique.
 59    /// </summary>
 60    public static Dictionary<TKey, TValue> Merge<TKey, TValue>(this IEnumerable<IDictionary<TKey, TValue>> other)
 61        where TKey : notnull
 62    {
 963        ArgumentNullException.ThrowIfNull(other);
 64
 965        var result = new Dictionary<TKey, TValue>();
 66
 6067        foreach (var dictionary in other)
 68        {
 8469            foreach (var pair in dictionary)
 2170                result[pair.Key] = pair.Value;
 71        }
 72
 973        return result;
 74    }
 75}
 76