48

I have an 'optional' parameter on a method that is a KeyValuePair. I wanted an overload that passes null to the core method for this parameter, but in the core method, when I want to check if the KeyValuePair is null, I get the following error:

Operator '!=' cannot be applied to operands of type System.Collections.Generic.KeyValuePair<string,object>' and '<null>. 

How can I not be allowed to check if an object is null?

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • Can you give a code sample that shows this error? – Jay Bazuzi Mar 15 '09 at 16:51
  • 2
    See also: http://stackoverflow.com/questions/1335419/how-can-i-make-sure-that-firstordefaultkeyvaluepair-has-returned-a-value –  Jul 01 '11 at 03:30

5 Answers5

81

KeyValuePair<K,V> is a struct, not a class. It's like doing:

int i = 10;
if (i != null) ...

(Although that is actually legal, with a warning, due to odd nullable conversion rules. The important bit is that the if condition will never be true.)

To make it "optional", you can use the nullable form:

static void Foo(KeyValuePair<object,string>? pair)
{
    if (pair != null)
    {
    }
    // Other code
}

Note the ? in KeyValuePair<object,string>?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    "(i != null)" does generate a warning, but sadly there is no such warning for a user-defined value type that overloads Equals/==/!= in accordance with MSDN guidelines. Very confusing when I implement "struct Foo" and my client expects "if (someFoo != null)" to be a reasonable way to check if someFoo has a value. :-p – yoyo Feb 22 '14 at 22:17
13

I'm answering this despite its age because it is the 1st Google result for "test keyvaluepair to null"

The answer specified is correct, but it doesn't completely answer the problem, at least the one I was having, where I need to test the existence of the KeyValuePair and move on to another check of the Dictionary if it doesn't exist the way I'm expecting.

Using the method above didn't work for me because the compiler chokes on getting KeyValuePair.Value of KeyValuePair<>?, so it is better to utilize default(KeyValuePair<>) as seen in this question + answers. The default for KeyValuePair

Community
  • 1
  • 1
eudaimos
  • 665
  • 7
  • 11
6

Because KeyValuePair is structure (a value type), you can only compare nulls on reference types.

I'm guessing you haven't written that extra overload yet. It will fail when you attempt to pass null as the value of a KeyValuePair.

AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
  • This isn't entirely true. `DateTime.Now == null` will not throw an exception, nor will `5 == null`. But `new KeyValuePair("", "") == null` will throw the exception. I wonder what other types besides `KeyValuePair` are affected by this. – ryanoshea Jul 23 '18 at 14:55
3

KeyValuePair<K,V> is a struct and hence can't be null under any circumstances. Only a reference type can be null.

If you're trying to catch an incorrect value you'll have to validate the Key and Value members individually.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
-9

You can actually use KeyValuePair.IsNull() to determine if the KeyValuePair has been set.

Jeremy
  • 1