Last active
December 11, 2015 09:58
-
-
Save hagbarddenstore/4582995 to your computer and use it in GitHub Desktop.
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
public static class DictionaryExtensions | |
{ | |
private static readonly Random Generator = new Random(); | |
public static TValue GetRandom<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) | |
{ | |
var randomKey = dictionary.Keys.ToList()[Generator.Next(0, dictionary.Keys.Count)]; | |
return dictionary[randomKey]; | |
} | |
/// <summary> | |
/// Merge one dictionary with another. | |
/// </summary> | |
/// <param name="current"> | |
/// The current. | |
/// </param> | |
/// <param name="overrides"> | |
/// The overrides. | |
/// </param> | |
public static void Merge<TKey, TValue>(this IDictionary<TKey, TValue> current, IEnumerable<KeyValuePair<TKey, TValue>> overrides) | |
{ | |
foreach (var @override in overrides) | |
{ | |
if (current.ContainsKey(@override.Key)) | |
{ | |
current[@override.Key] = @override.Value; | |
} | |
else | |
{ | |
current.Add(@override.Key, @override.Value); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment