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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:34:35+00:00 2026-05-12T18:34:35+00:00

I am talking about C# language here. Definition of Object.Equals(Object) method in msdn is:

  • 0

I am talking about C# language here.

Definition of Object.Equals(Object) method in msdn is:

Determines whether the specified
Object is equal to the current Object.

If two objects are equal it returns true, however if they are null it returns false:

x.Equals(a null reference (Nothing in
Visual Basic)) returns false.

Why? Because null is not an object.

A NullReferenceException is thrown if the object paremeter is null.

and also we have this:

x.Equals(y) returns the same value as
y.Equals(x).

No problem at all till here. It is very similar to Java. But C# also provides a System.Nullable struct for non-nullable types. As far as I know, a struct is an object. It inherits Object.Equals method.

If I have a struct like this:

struct Car
    {
        public string Make;
        public string Model;
        public uint Year;

        public Car(string make, string model, uint year)
        {
            Make = make;
            Model = model;
            Year = year;
        }
    }

And create four instances:

Car car1 = new Car("make", "model", 2009);
Car car2 = new Car("make", "model", 2009);
Car car3 = new Car("make", "model", 2008);

car1.Equals(car2); // will return true
car1.Equals(car3); // will return false;

And as far as I know we can’t set a struct to a null value. But System.Nullable is a struct and we can do compile this without any errors:

int? i = null;

(I hope that someone can explain this also. Is it a struct or something else?)

My real question is:

i.Equals(null); // returns true!

(Normally x.Equals(y) = y.Equals(x) Of course null.Equals(i) is not valid here… )

Obviously Object.Equals method is overridden here. Maybe it is documented and this is specified. But is this approach correct/nice ? If so what is the difference between == and Equals method for Nullable values?

  • 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-12T18:34:36+00:00Added an answer on May 12, 2026 at 6:34 pm

    I think your confusion is rooted in the following line

    i? = null;
    

    This does not actually create a null value variable. It’s essentially syntatic sugar for the following

    Nullable<int> i = new Nullable<int>();
    

    The resulting property HasValue on i will have the value false. It is not null but instead a value type with empty values. Or just an empty nullable. IMHO, the best way to think of this is that null is convertible to an empty Nullable<T> for any given T.

    Knowing that it makes the line i.Equals(null) a bit easier to understand. It’s syntatic sugar for the following

    Nullable<int> i = new Nullable<int>();
    i.Equals(null);
    

    The type Nullable<T> only overrides Equals(object). The implementation of this method though considers a null value to be Equal to an empty nullable value. So it’s behaving correctly.

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

Sidebar

Related Questions

What I'm talking about here are nested classes. Essentially, I have two classes that
When verbally talking about methods, I'm never sure whether to use the word argument
Talking about hibernate and others ORMs, the ORMs evangelists talk about SQL like the
I'm obviously not talking about a full solution, but just a good starting point
I'm not talking about bug tracking software (like Bugzilla or Jira). I'm looking for
I'm not talking about tools that let one view a page in combinations of
I'm talking about an action game with no upper score limit and no way
I'm not talking about the kind you get in college, but rather implementing progress
Now that everyone is talking about MVC, I notice that the business rules are
I'm not talking about a post build event for a project. Rather, I want

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.