using System.Collections.Generic;
public static void Main()
var myDict = new Dictionary<int, string>()
var result = myDict.GetValue(5, "baz");
Console.WriteLine(result);
public static class DictionaryExtensions
public static TValue GetValue<TKey, TValue>(this IDictionary<TKey, TValue> source, TKey key, TValue defaultValue)
return (source.ContainsKey(key) ? source[key] : defaultValue);
public static class WebExtensions
public static T GetValue<T>(this HttpContext context, string name, T defaultValue)
object value = context.Request.Form[name] ?? context.Request.QueryString[name];
if (value == null) return defaultValue;