Created
May 16, 2021 23:03
-
-
Save DarkStoorM/3d996bae910dd086c7234a47c461e777 to your computer and use it in GitHub Desktop.
[C#] Merge two Dictionaries together
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
public static class DictionaryExtensions | |
{ | |
/// <summary> | |
/// <para />Merges this dictionary with another dictionary | |
/// <example> | |
/// <code> | |
/// Dictionary<string, int> 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