Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 635939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:27:38+00:00 2026-05-13T20:27:38+00:00

I have a few places where I need to compare 2 (nullable) values, to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:27:38+00:00Added an answer on May 13, 2026 at 8:27 pm

    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;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have quite a few places where I need to bind form controls directly
I have a few places in the code where I need to use the
I have few PC's that run on slow Internet connection network + they use
I have few fields and I want the values inside those fields to get
I have few classes that I need to annotate with a name so I
I have seen process.nextTick used in a few places and can't quite tell what
There are a few ways to do a merge sort, but I specifically need
I ultimately have two areas with a few questions each about Entity Framework, but
I have a label that I want to use it in few places. It
I have searched quite a few places but i haven't got up with any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.