Skip to content

Instantly share code, notes, and snippets.

@DarkStoorM
Created May 16, 2021 23:03
Show Gist options
  • Save DarkStoorM/3d996bae910dd086c7234a47c461e777 to your computer and use it in GitHub Desktop.
Save DarkStoorM/3d996bae910dd086c7234a47c461e777 to your computer and use it in GitHub Desktop.
[C#] Merge two Dictionaries together
using System.Collections.Generic;
using System.Linq;
public static class DictionaryExtensions
{
/// <summary>
/// <para />Merges this dictionary with another dictionary
/// <example>
/// <code>
/// Dictionary&lt;string, int&gt; dictionary = dictionary.MergeWith(otherDictionary);
/// </code>
/// </example>
/// </summary>
/// <returns>
/// <para />Merged dictionary with provided dictionary as a parameter
/// </returns>
public static Dictionary<TKey, TValue> MergeWith<TKey, TValue>(this Dictionary<TKey, TValue> thisDictionary, Dictionary<TKey, TValue> otherDictionary)
=> thisDictionary.Union(otherDictionary).ToDictionary(k => k.Key, v => v.Value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment