What is default value of `KeyValuePair<string, int>`?
c#, linq
Solution
The default value of any type T is `default(T)`, so for 100% theoretical accuracy you would write
if (!mapping.Equals(default(KeyValuePair<string, int>))) {
// ...
}
Since `KeyValuePair` is a `struct` (i.e. a value type) you cannot compare it to `null`. Irrespective of that, comparing values with `==` is wrong as a default approach because if used on reference types it normally checks for reference equality.
Problem
What is default value of `KeyValuePair<string, int>`? e.g. I am running a LINQ query and returning `FirstOrDefault()` value from it ``` KeyValuePair<string, int> mapping = (from p in lstMappings where p.Key.Equals(dc.ColumnName, StringComparison.InvariantCultureIgnoreCase) select p).FirstOrDefault(); if (mapping != null) { } ``` how to check if `mapping` object is null/blank (I am getting compile time error in above code `Operator '!=' cannot be applied to operands of type 'System.Collections.Generic.KeyValuePair<string,int>' and '<null>'`) PS: `lstMappings` is of type ``` List<KeyValuePair<string, int>> ```