In .Net 2.5 I can usually get an equality comparison (==) between a value and its type default
if (myString == default(string))
However I get the following exception when I try to run an equality comparison on a default KeyValuePair and a KeyValuePair
Code Sample (from a pre-extension method, proto-lambda static ListUtilities class :) )
public static TKey
FirstKeyOrDefault<TKey, TValue>(Dictionary<TKey, TValue> lookups,
Predicate<KeyValuePair<TKey, TValue>> predicate)
{
KeyValuePair<TKey, TValue> pair = FirstOrDefault(lookups, predicate);
return pair == default(KeyValuePair<TKey, TValue>) ?
default(TKey) : pair.Key;
}
Exception:
Operator '==' cannot be applied to
operands of type
'System.Collections.Generic.KeyValuePair<string,object>'
and
'System.Collections.Generic.KeyValuePair<string,object>'
Is it because, as a struct, the KeyValuePair is not nullable? If this is the case, why, as, presumably, default was implemented to handle not nullable types?
EDIT
For the record, I chose @Chris Hannon as selected answer, as he gave me what I was looking for, the most elegant option, and a succinct explanation, however I do encourage reading @Dasuraga for a very comprehensive explanation as to why this is the case
question from:
https://stackoverflow.com/questions/6379915/why-cant-i-compare-a-keyvaluepairtkey-tvalue-with-default 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…