78

I have a few places where I need to compare 2 (nullable) values, to see if they're the same.

I think there should be something in the framework to support this, but can't find anything, so instead have the following:

public static bool IsDifferentTo(this bool? x, bool? y)
{
    return (x.HasValue != y.HasValue) ? true : x.HasValue && x.Value != y.Value;
}

Then, within code I have if (x.IsDifferentTo(y)) ...

I then have similar methods for nullable ints, nullable doubles etc.

Is there not an easier way to see if two nullable types are the same?

Update:

Turns out that the reason this method existed was because the code has been converted from VB.Net, where Nothing = Nothing returns false (compare to C# where null == null returns true). The VB.Net code should have used .Equals... instead.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
David_001
  • 5,703
  • 4
  • 29
  • 55

8 Answers8

90

C# supports "lifted" operators, so if the type (bool? in this case) is known at compile you should just be able to use:

return x != y;

If you need generics, then EqualityComparer<T>.Default is your friend:

return !EqualityComparer<T>.Default.Equals(x,y);

Note, however, that both of these approaches use the "null == null" approach (contrast to ANSI SQL). If you need "null != null" then you'll have to test that separately:

return x == null || x != y;
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
48

Nullable.Equals<T>?

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288
  • 3
    Other answers are claiming that `x.Equals(y)` and `x == y` work just fine when x and/or y are instances of `Nullable`. How is the linked method different? When should it be used? – MarioDS Mar 29 '16 at 14:02
  • 4
    Or `Nullabe.Compare` if you are looking for an `IComparable` impl – Sepehr Oct 28 '16 at 17:09
31
if (x.Equals(y)) 
Kashif
  • 14,071
  • 18
  • 66
  • 98
  • 2
    Can you please tell me why this downvote. Just wanted to know where I am wrong. – Kashif Feb 26 '10 at 16:01
  • 1
    I didn't downvote you, but what happens if x is null? Invoking a method on a null reference would probably result in a NullReferenceException. – Chris Shouts Mar 30 '10 at 16:08
  • 15
    @Chris: You should upvote the answer again. A nullable type `T?` is NOT a reference type, it's a `System.Nullable`. And this is a struct, so: a value type. Muhammad's code is perfectly legal and won't throw a NullReferenceException if x is null. – Slauma Nov 15 '10 at 19:36
  • 2
    @Chris: Oh, sorry, sorry, I misread that you did NOT downvote the answer, so you are free to upvote or not ;) – Slauma Nov 15 '10 at 19:48
6

You can use the static Equals method on System.Object:

var equal = object.Equals(objA, objB);
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
6

Use Compare:

http://msdn.microsoft.com/en-us/library/dxxt7t2a.aspx

Lucero
  • 59,176
  • 9
  • 122
  • 152
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
  • 1
    You don't need compare to check for equality; this is only needed for real comparison such as for sorting. – Lucero Feb 26 '10 at 12:58
5

Just use ==, or .Equals().

Lucero
  • 59,176
  • 9
  • 122
  • 152
3

I wanted to find how to compare two nullable int on C#, but I always get this link after search, so if someone needs to compare exactly two nullable int, then this can be helpful

a.GetValueOrDefault(int.MinValue).CompareTo(b.GetValueOrDefault(long.MinValue));

hdd42
  • 66
  • 4
2
(x?? 0).Equals(y)

will handle null as well as equals.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Genius_x
  • 41
  • 6