using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SuperSocket.Common { /// /// Extension class for IDictionary /// public static class DictionaryExtension { /// /// Gets the value by key. /// /// /// The dictionary. /// The key. /// public static T GetValue(this IDictionary dictionary, object key) where T : new() { T defaultValue = default(T); return GetValue(dictionary, key, defaultValue); } /// /// Gets the value by key and default value. /// /// /// The dictionary. /// The key. /// The default value. /// public static T GetValue(this IDictionary dictionary, object key, T defaultValue) { object valueObj; if (!dictionary.TryGetValue(key, out valueObj)) { return defaultValue; } else { return (T)valueObj; } } } }