< Summary - SonghayCore

Information
Class: Songhay.Extensions.IDictionaryExtensions
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/IDictionaryExtensions.cs
Line coverage
100%
Covered lines: 24
Uncovered lines: 0
Coverable lines: 24
Total lines: 69
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
ToNameValueCollection(...)100%4100%
TryGetValueWithKey(...)100%1100%
TryGetValueWithKey(...)100%6100%

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/IDictionaryExtensions.cs

#LineLine coverage
 1namespace Songhay.Extensions;
 2
 3/// <summary>
 4/// Extensions of <see cref="IDictionary{TKey, TValue}"/>.
 5/// </summary>
 6// ReSharper disable once InconsistentNaming
 7public static class IDictionaryExtensions
 8{
 9    /// <summary>
 10    /// Converts the <see cref="IDictionary{TKey, TValue}"/>
 11    /// to the <see cref="NameValueCollection"/>.
 12    /// </summary>
 13    /// <typeparam name="TKey">The type of the key.</typeparam>
 14    /// <typeparam name="TValue">The type of the value.</typeparam>
 15    /// <param name="dictionary">The set.</param>
 16    /// <remarks>
 17    /// For detail, see https://stackoverflow.com/a/7230446/22944
 18    /// </remarks>
 19    public static NameValueCollection ToNameValueCollection<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
 120    {
 121        var nameValueCollection = new NameValueCollection();
 22
 1123        foreach (var kvp in dictionary)
 424        {
 425            string? value = null;
 426            if (kvp.Value != null)
 427                value = kvp.Value.ToString();
 28
 429            nameValueCollection.Add(kvp.Key!.ToString(), value);
 430        }
 31
 132        return nameValueCollection;
 133    }
 34
 35    /// <summary>
 36    /// Tries to get value with the specified key.
 37    /// </summary>
 38    /// <typeparam name="TKey">The type of the key.</typeparam>
 39    /// <typeparam name="TValue">The type of the value.</typeparam>
 40    /// <param name="dictionary">The dictionary.</param>
 41    /// <param name="key">The key.</param>
 42    public static TValue? TryGetValueWithKey<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) =>
 243        dictionary.TryGetValueWithKey(key, throwException: false);
 44
 45    /// <summary>
 46    /// Tries to get value with the specified key.
 47    /// </summary>
 48    /// <typeparam name="TKey">The type of the key.</typeparam>
 49    /// <typeparam name="TValue">The type of the value.</typeparam>
 50    /// <param name="dictionary">The dictionary.</param>
 51    /// <param name="key">The key.</param>
 52    /// <param name="throwException">if set to <c>true</c> [throw exception].</param>
 53    public static TValue? TryGetValueWithKey<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey? key,
 54        bool throwException)
 355    {
 356        ArgumentNullException.ThrowIfNull(dictionary);
 357        ArgumentNullException.ThrowIfNull(key);
 58
 359        var test = dictionary.TryGetValue(key, out var value);
 60
 361        return value switch
 362        {
 363            null when !test && throwException => throw new NullReferenceException(
 164                $"The expected value from key, {key}, is not here."),
 265            null when !test => default,
 166            _ => value
 367        };
 268    }
 69}