Why can't I compare a KeyValuePair with default

后端 未结 6 2030
Happy的楠姐
Happy的楠姐 2020-12-30 19:25

In .Net 2.5 I can usually get an equality comparison (==) between a value and its type default

if (myString == default(string))

However I g

6条回答
  •  再見小時候
    2020-12-30 19:56

    It fails for the same reason as the following:

    var kvp = new KeyValuePair("a","b");
    var res = kvp == kvp;
    

    The clue is in the error message, naturally. (It has nothing to do with default).

    Operator '==' cannot be applied to operands of type 'System.Collections.Generic.KeyValuePair' and 'System.Collections.Generic.KeyValuePair'
    

    The operator == is not defined for KeyValuePair.

    Error messages FTW.

    Happy coding.

提交回复
热议问题