Why can't I compare a KeyValuePair<TKey, TValue> with default

johnc picture johnc · Jun 17, 2011 · Viewed 20.8k times · Source

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

Answer

Chris Hannon picture Chris Hannon · Jun 17, 2011

This happens because KeyValuePair<TKey, TValue> does not define a custom == operator and is not included in the predefined list of value types that can use it.

Here is a link to the MSDN documentation for that operator.

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise.

Your best bet for an equality check in this case, because this is not a struct you have control over, is to call default(KeyValuePair<TKey,TValue>).Equals(pair) instead.