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 6247417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:52:03+00:00 2026-05-24T12:52:03+00:00

Almost every time I want to check object’s equality to null I use the

  • 0

Almost every time I want to check object’s equality to null I use the normal equality check operation

if (obj == null)

Recently I noticed that I’m using the Object.Equals() more often

if (Object.Equals(obj, null))

and while reading about null checking I fount this Is ReferenceEquals(null, obj) the same thing as null == obj?

if (ReferenceEquals(null, obj))

Whats the difference? and where/when to use each one? plus I found that the last two checks look like the same according to their summary

  • 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-24T12:52:03+00:00Added an answer on May 24, 2026 at 12:52 pm

    Object.Equals(x, y) will:

    • Return true if x and y are both null
    • Return false if exactly one of x or y is null
    • Otherwise call either x.Equals(y) or y.Equals(x) – it shouldn’t matter which. This means that whatever polymorphic behaviour has been implemented by the execution-time type of the object x or y refers to will be invoked.

    ReferenceEquals will not call the polymorphic Equals method. It just compares references for equality. For example:

    string x = new StringBuilder("hello").ToString();
    string y = new StringBuilder("hello").ToString();
    Console.WriteLine(Object.Equals(x, y)); // True
    Console.WriteLine(Object.ReferenceEquals(x, y)); // False
    Console.WriteLine(x == y); // True due to overloading
    

    Now if you’re only checking for nullity, then you don’t really want the polymorphic behaviour – just reference equality. So feel free to use ReferenceEquals.

    You could also use ==, but that can be overloaded (not overridden) by classes – it is in the case of string, as shown above. The most common case for using ReferenceEquals in my experience is when you’re implementing ==:

    public bool operator ==(Foo x1, Foo x2)
    {
        if (ReferenceEquals(x1, x2))
        {
            return true;
        }
        if (ReferenceEquals(x1, null) || ReferenceEquals(x2, null))
        {
            return false;
        }
        return x1.Equals(x2);
    }
    

    Here you really don’t want to call the == implementation, because it would recurse forever – you want the very definite reference equality semantics.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

well for some strange reason IE gives me and InvalidAuthenticityToken error almost every time
I don't edit CSS very often, and almost every time I need to go
Almost every time I close SQL Server Management Studio I get a prompt that
Almost every time I commit to SVN I keep getting a checksum mismatch error.
I Have a hosted service on Azure, every time I want to put the
Is a table locked every time I insert something in it? Because I want
Almost every Java book I read talks about using the interface as a way
Almost every Python web framework has a simple server that runs a wsgi application
Almost every new Java-web-project is using a modern MVC-framework such as Struts or Spring
Almost every flash player has an option to display how much of buffer (or

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.