我想爲IDictionary
- GetValue
做一個超酷擴展,如果沒有設置,默認值爲null。這裏是我想出了一個代碼(不工作):C#中擴展方法中的可枚舉嵌套類型
public static TValue GetValue<TKey, TValue> (this IDictionary<TKey,
TValue> dictionary, TKey key, TValue defaultValue = null)
{
TValue value;
return dictionary.TryGetValue(key, out value)
? value
: defaultValue;
}
如何使這個只有nullables
? (比如,不包括int
等)。
很好地向OP說明限制真正打算做什麼。 「可空」意味着它們必須是引用類型。 – Yuck