Why can't I compare a KeyValuePair with default

后端 未结 6 2013
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 20:17

    This happens because KeyValuePair 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).Equals(pair) instead.

提交回复
热议问题