Skip to content

Instantly share code, notes, and snippets.

@hagbarddenstore
Last active December 11, 2015 09:58
Show Gist options
  • Save hagbarddenstore/4582995 to your computer and use it in GitHub Desktop.
Save hagbarddenstore/4582995 to your computer and use it in GitHub Desktop.
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