< Summary

Information
Class: MyNet.Collections.DictionaryExtensions
Assembly: MyNet.Collections
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Collections/Extensions/DictionaryExtensions.cs
Tag: 323_28699572109
Line coverage
100%
Covered lines: 15
Uncovered lines: 0
Coverable lines: 15
Total lines: 78
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/85372080fe102cd9ee155ceab49ae000e7f66103/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
 629            if (!dictionary.TryGetValue(key, out var value))
 30            {
 331                value = factory();
 332                dictionary.Add(key, value);
 33            }
 34
 635            return value;
 36        }
 37
 38        /// <summary>
 39        /// Gets the value for the specified key or returns a default when missing.
 40        /// </summary>
 41        public TValue? GetOrDefault(TKey key, TValue? defaultValue = default)
 42        {
 943            ArgumentNullException.ThrowIfNull(dictionary);
 44
 945            return dictionary.TryGetValue(key, out var value) ? value : defaultValue;
 46        }
 47
 48        /// <summary>
 49        /// Merges two dictionaries into a new dictionary. Keys are expected to be unique.
 50        /// </summary>
 51        public Dictionary<TKey, TValue> Merge(IDictionary<TKey, TValue> dictionary2)
 52        {
 653            ArgumentNullException.ThrowIfNull(dictionary);
 54
 655            return new[] { dictionary, dictionary2 }.Merge();
 56        }
 57    }
 58
 59    /// <summary>
 60    /// Merges multiple dictionaries into a new dictionary. Keys are expected to be unique.
 61    /// </summary>
 62    public static Dictionary<TKey, TValue> Merge<TKey, TValue>(this IEnumerable<IDictionary<TKey, TValue>> other)
 63        where TKey : notnull
 64    {
 965        ArgumentNullException.ThrowIfNull(other);
 66
 967        var result = new Dictionary<TKey, TValue>();
 68
 6069        foreach (var dictionary in other)
 70        {
 8471            foreach (var pair in dictionary)
 2172                result[pair.Key] = pair.Value;
 73        }
 74
 975        return result;
 76    }
 77}
 78