BryanWilhite / SonghayCore

core reusable, opinionated concerns for *all* 🧐 of my C# projects
http://songhayblog.azurewebsites.net/
MIT License
1 stars 0 forks source link

add new members to `IDictionaryExtensions` #156

Closed BryanWilhite closed 7 months ago

BryanWilhite commented 1 year ago

these members come in handy when passing attributes to ASP.NET HTML helper methods:

/// <summary>
    /// Extensions of <see cref="IDictionary{TKey, TValue}"/>
    /// </summary>
    public static class IDictionaryExtensions
    {
        /// <summary>
        /// Adds the specified key and value
        /// or overwites the value when the key exists
        /// in the specified <see cref="IDictionary{TKey, TValue}"/>.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="dictionary">the <see cref="IDictionary{TKey, TValue}"/></param>
        /// <param name="key">the key</param>
        /// <param name="value">the value</param>
        public static void AddOrOverWrite<TKey, TValue>(this IDictionary<TKey, TValue>? dictionary, TKey? key, TValue? value) where TKey : notnull
        {
            ArgumentNullException.ThrowIfNull(dictionary);

            if (key == null) return;

            if(dictionary.ContainsKey(key))
            {
                dictionary[key] = value!;
            }
            else
            {
                dictionary.Add(key, value!);
            }
        }

        /// <summary>
        /// Returns the <see cref="IDictionary{TKey, TValue}"/>
        /// with the specified pair.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="dictionary">the <see cref="IDictionary{TKey, TValue}"/></param>
        /// <param name="key">the key</param>
        /// <param name="value">the value</param>
        public static IDictionary<TKey, TValue> WithPair<TKey, TValue>(this IDictionary<TKey, TValue>? dictionary, TKey? key, TValue? value) where TKey : notnull
        {
            ArgumentNullException.ThrowIfNull(dictionary);

            dictionary.AddOrOverWrite(key, value!);

            return dictionary;
        }

        /// <summary>
        /// Returns the <see cref="IDictionary{TKey, TValue}"/>
        /// with the specified pairs.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="dictionary">the <see cref="IDictionary{TKey, TValue}"/></param>
        /// <param name="pairs">The pairs to add.</param>
        public static IDictionary<TKey, TValue> WithPairs<TKey, TValue>(this IDictionary<TKey, TValue>? dictionary, IEnumerable<KeyValuePair<TKey, TValue>>? pairs) where TKey : notnull
        {
            ArgumentNullException.ThrowIfNull(dictionary);

            if (pairs == null) return dictionary;

            foreach (var pair in pairs)
            {
                dictionary.AddOrOverWrite(pair.Key, pair.Value);
            }

            return dictionary;
        }

        /// <summary>
        /// Converts the <see cref="IDictionary{TKey, TValue}"/>
        /// to a shallow clone.
        /// </summary>
        /// <typeparam name="TKey">The type of the key.</typeparam>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="dictionary">the <see cref="IDictionary{TKey, TValue}"/></param>
        /// <remarks>
        /// For more detail see “Clone a Dictionary in C#”
        /// [https://www.techiedelight.com/clone-a-dictionary-in-csharp/]
        /// </remarks>
        public static IDictionary<TKey, TValue> ToShallowClone<TKey, TValue>(this IDictionary<TKey, TValue>? dictionary) where TKey: notnull
        {
            ArgumentNullException.ThrowIfNull(dictionary);

            return dictionary.ToDictionary(i => i.Key, j => j.Value);
        }
    }
BryanWilhite commented 7 months ago

the AddOrOverWrite method appears to be superfluous as dictionary[key] = value is apparently the equivalent