482

I have an object of the type IEnumerable<KeyValuePair<T,U>> keyValueList, I am using

 var getResult= keyValueList.SingleOrDefault();
 if(getResult==/*default */)
 {
 }
 else
 {
 } 

How can I check whether getResult is the default, in case I can't find the correct element?

I can't check whether it is null or not, because KeyValuePair is a struct.

yoozer8
  • 7,361
  • 7
  • 58
  • 93
Graviton
  • 81,782
  • 146
  • 424
  • 602

8 Answers8

659

Try this:

if (getResult.Equals(new KeyValuePair<T,U>()))

or this:

if (getResult.Equals(default(KeyValuePair<T,U>)))
Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • 5
    You can use the concept of the "new" option here, but avoid having to re-specify the type: if (getResult.Equals(Activator.CreateInstance(getResult.GetType()))) – kevinpo Mar 21 '14 at 14:42
  • 18
    i think `if (getResult.Equals(default))` also works – Simon Mar 04 '20 at 05:57
  • 9
    @Simon no it does not. `default` equals to null. And `default(KeyValuePair)` is an actual KeyValuePair that contains `null, null`. Experienced this myself. – Yokovaski May 13 '20 at 10:18
  • Backing @Simon here. My KVP type is `KeyValuePair`. The default value returned by `keyValueList.SingleOrDefault()` contains `0,null`. When I do `getResult.Equals(default)`, it returns `true`. Tested in net framework 4.7.2. – Noctis Tong Nov 04 '20 at 03:55
  • 3
    I have just checked this with .net core 5 and getResult.Equals(default) will return false when getResult.Equals(default(KeyValuePair)) returns true. – Tod Jun 04 '21 at 15:47
116

You can create a general (and generic) extension method, like this one:

public static class Extensions
{
    public static bool IsDefault<T>(this T value) where T : struct
    {
        bool isDefault = value.Equals(default(T));

        return isDefault;
    }
}

Usage:

// We have to set explicit default value '0' to avoid build error:
// Use of unassigned local variable 'intValue'
int intValue = 0;
long longValue = 12;
KeyValuePair<String, int> kvp1 = new KeyValuePair<String, int>("string", 11);
KeyValuePair<String, int> kvp2 = new KeyValuePair<String, int>();
List<KeyValuePair<String, int>> kvps = new List<KeyValuePair<String, int>> { kvp1, kvp2 };
KeyValuePair<String, int> kvp3 = kvps.FirstOrDefault(kvp => kvp.Value == 11);
KeyValuePair<String, int> kvp4 = kvps.FirstOrDefault(kvp => kvp.Value == 15);

Console.WriteLine(intValue.IsDefault()); // results 'True'
Console.WriteLine(longValue.IsDefault()); // results 'False'
Console.WriteLine(kvp1.IsDefault()); // results 'False'
Console.WriteLine(kvp2.IsDefault()); // results 'True'
Console.WriteLine(kvp3.IsDefault()); // results 'False'
Console.WriteLine(kvp4.IsDefault()); // results 'True'
davmos
  • 9,324
  • 4
  • 40
  • 43
pholpar
  • 1,725
  • 2
  • 14
  • 23
32

Try this:

KeyValuePair<string,int> current = this.recent.SingleOrDefault(r => r.Key.Equals(dialog.FileName) == true);

if (current.Key == null)
    this.recent.Add(new KeyValuePair<string,int>(dialog.FileName,0));
Jim Counts
  • 12,535
  • 9
  • 45
  • 63
Peter
  • 329
  • 3
  • 2
  • 5
    Of course, that only works if you're never going to (accidentally or purposefully) add null as a key. But I'd say that's true the vast majority of the time, so good idea. That is definitely a way simpler check than most of the checks here. – neminem Dec 06 '13 at 23:52
  • 4
    As long as the key is nullable. – Uri Abramson Nov 19 '14 at 08:41
10
if(getResult.Key.Equals(default(T)) && getResult.Value.Equals(default(U)))
brimble2010
  • 17,796
  • 7
  • 28
  • 45
ChaosPandion
  • 77,506
  • 18
  • 119
  • 157
9

I recommend more understanding way using extension method:

public static class KeyValuePairExtensions
{
    public static bool IsNull<T, TU>(this KeyValuePair<T, TU> pair)
    {
        return pair.Equals(new KeyValuePair<T, TU>());
    }
}

And then just use:

var countries = new Dictionary<string, string>
{
    {"cz", "prague"},
    {"de", "berlin"}
};

var country = countries.FirstOrDefault(x => x.Key == "en");

if(country.IsNull()){

}
Miroslav Holec
  • 3,139
  • 25
  • 22
9

From your original code it looks like what you want is to check if the list was empty:

var getResult= keyValueList.SingleOrDefault();
if (keyValueList.Count == 0)
{
   /* default */
}
else
{
}
horgh
  • 17,918
  • 22
  • 68
  • 123
8

To avoid the boxing of KeyValuePair.Equals(object) you can use a ValueTuple.

if ((getResult.Key, getResult.Value) == default)
Jonas Nyrup
  • 2,376
  • 18
  • 25
2

It may suffice, for your application, to check if either the Key, or the Value are default.. For example if you have a KeyValuePair<int,string> mapping positive nonzero integers to strings, and the strings are never null, then either:

if(getResult.Key == default)

if(getResult.Value == default)

Would be an indicator that a default KVP has been returned

In essence, there may be no need to test the whole KVP for being default, if just one test will do; consider it for your use case

Caius Jard
  • 72,509
  • 5
  • 49
  • 80