Why can't I compare a KeyValuePair with default

后端 未结 6 1907
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:02

    This goes in a slightly different direction, but I am presuming you queried a Dictionary to get this result, and then you want to check if it returned a valid result or not.

    I found the better method of doing this was to query out the actual value instead of the whole KeyValuePair, like this:

    var valitem = MyDict.Values.FirstOrDefault(x=> x.Something == aVar);

    Now you can check if valitem is null or not. Again, it doesn't directly answer your question, but offers what might be a alternative approach to your intended goal.

提交回复
热议问题