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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:40:05+00:00 2026-05-12T17:40:05+00:00

What decision should I take about Equals(), ReferenceEquals(), and == from the following results?

  • 0

What decision should I take about Equals(), ReferenceEquals(), and == from the following results? What do they produce actually?

#region
int integer = 1;
int integer2 = integer;

bool referenceEquality = (integer == integer2);//true
bool valueEquality = integer.Equals(integer2);//true
bool valueEqualityMore = object.Equals(integer, integer2);//true
bool valueEqualityMoreMore = object.ReferenceEquals(integer, integer2);//false
#endregion

#region
int integer = 1;
int integer2 = 1;

bool referenceEquality = (integer == integer2);//true
bool valueEquality = integer.Equals(integer2);//true
bool valueEqualityMore = object.Equals(integer, integer2);//true
bool valueEqualityMoreMore = object.ReferenceEquals(integer, integer2);//false
#endregion

#region
MyClass obj = new MyClass(1, "Hello");
MyClass obj2 = obj;

bool referenceEquality = (obj == obj2);//true
bool valueEquality = obj.Equals(obj2);//true
bool valueEqualityMore = object.Equals(obj, obj2);//true
bool valueEqualityMoreMore = object.ReferenceEquals(obj, obj2);//true            
#endregion

#region
MyClass obj = new MyClass(1, "Hello");
MyClass obj2 = new MyClass(1, "Hello");

bool referenceEquality = (obj == obj2);//false
bool valueEquality = obj.Equals(obj2);//false
bool valueEqualityMore = object.Equals(obj, obj2);//false
bool valueEqualityMoreMore = object.ReferenceEquals(obj, obj2);//false
#endregion

Hell! I understand nothing.

To me referenceEquals() of the 1st block should return true.
== in the second block should return false (as the references are different).
And, both of the Equals() in the 4th block should return true (as their values are same).

  • 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-12T17:40:05+00:00Added an answer on May 12, 2026 at 5:40 pm

    The first point of confusion you seem to be having is that with value types, i.e. int, float, DateTime, the == operator is value equality. With reference types, == is (by default, see below) reference equality. This would explain the disparity of answers in your first two integer cases.

    Secondly, the default implementation of Equals() tests reference equality, not value equality. So since it appears that MyClass doesn’t override Equals(), that explains the disparity of answers between your reference cases.

    Additionally, many reference types, such as String, override the == operator to provide value-equality semantics. So your best bet, until you memorize which types are which, is to look up the documentation.

    In short:

    • Value types
      • == is value-equality (for framework types)
      • Not a reference type, so reference equality is meaningless
    • Reference types
      • ReferenceEquals() is always reference equality
      • == is reference equality by default, but can be (and often is for framework types) overridden to provide value equality
      • Equals() is reference equality by default, but can be (and often is for framework types) overridden to provide value equality
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the design decision to lean towards not returning an anonymous types from
This is about the design decision and understand the procs and cons for adopting
I've seen various posts about how to implement the deletion of dependent rows from
How can we take decision for viewstate and hidden field in ASP.NET. In my
I am at a point where I need to take the decision on what
I'm currently in the decision of under what license I should release a .NET
Which one of the following two pieces of code should be preferred over other?
I'm supposed to take two sentences each time and compute if they are similar.
Is the decision by Google similar to the one for SortedMultiSet (stackoverflow question) or
I built a slideshow/decision-making game in Flash but would like to try to redo

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.