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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:58:45+00:00 2026-05-15T13:58:45+00:00

While learning .net (by c#) i found 5 ways for checking equality between objects.

  • 0

While learning .net (by c#) i found 5 ways for checking equality between objects.

  1. The ReferenceEquals() method.
  2. The virtual Equals() method. (System.Object)
  3. The static Equals() method.
  4. The Equals method from IEquatable interface.
  5. The comparison operator == .

My question is :

  1. Why there are so many Equals() method along with comparison operator?
  2. Which one of the virtual Equals() or IEquatable’s Equals() sholud be used .. (say if we use our own collection classes)
  • 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-15T13:58:46+00:00Added an answer on May 15, 2026 at 1:58 pm

    1 – Reference equals checks if two reference type variables(classes, not structs) are referred to the same memory adress.

    2 – The virtual Equals() method checks if two objects are equivalent. Let us say that you have this class:

    class TestClass{
        public int Property1{get;set}
        public int Property2{get;set}
    
        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(TestClass))
                return false;
    
            var convertedObj = (TestClass)obj;
    
            return (convertedObj.Property1 == this.Property1 && convertedObj.Property2 == this.Property2);
        }
    }
    

    and you instantiate 2 objects from that class:

    var o1 = new TestClass{property1 = 1, property2 = 2}
    var o2 = new TestClass{property1 = 1, property2 = 2}
    

    although the two objects are not the same instance of TestClass, the call to o1.Equals(o2) will return true.

    3 – The static Equals method is used to handle problems when there is a null value in the check.
    Imagine this, for instance:

    TestClass o1 = null;
    var o2 = new TestClass{property1 = 1, property2 = 2}
    

    If you try this:

    o1.Equals(o2);
    

    you wil get a NullReferenceException, because o1 points to nothing.
    To adress this issue, you do this:

    Object.Equals(o1,o2);

    This method is prepared to handle null references.

    4 – The IEquatable interface is provided by .Net so you don’t need to do casts inside your Equals method.
    If the compiler finds out that you have implemented the interface in a class for the type you are trying to check for equality, it will give that method priority over the Object.Equals(Object) override.
    For instance:

    class TestClass : IEquatable<TestClass>
    {
        public int Property1 { get; set; }
        public int Property2 { get; set; }
    
        public override bool Equals(object obj)
        {
            if (obj.GetType() != typeof(TestClass))
                return false;
    
            var convertedObj = (TestClass)obj;
    
            return (convertedObj.Property1 == this.Property1 && convertedObj.Property2 == this.Property2);
        }
    
        #region IEquatable<TestClass> Members
    
        public bool Equals(TestClass other)
        {
            return (other.Property1 == this.Property1 && other.Property2 == this.Property2);
        }
    
        #endregion
    }
    

    now if we do this:

    var o1 = new TestClass{property1 = 1, property2 = 2}
    var o2 = new TestClass{property1 = 1, property2 = 2}
    o1.Equals(o2);
    

    The called method is Equals(TestClass), prior to Equals(Object).

    5 – The == operator usually means the same as ReferenceEquals, it checks if two variables point to the same memory adress.
    The gotcha is that this operator can be overrided to perform other types of checks.
    In strings, for instance, it checks if two different instances are equivalent.

    This is a usefull link to understand equalities in .Net better:

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

Sidebar

Related Questions

So I found some similarities between arrays and set notation while learning about sets
I am Learning cryptography in .net, why method 1 works while 2 fired argument
1) A while ago I’ve started learning Asp.Net, but then I’ve heard that Ajax
While learning Rails, I found this example in the Sam Ruby's book: app/controllers/line_items_controller.rb def
I'm a Java Developer wich is learning VB.net for a small project. While coding
I'm a newbie to Asp.net,learning from the Apress's Begining Asp.net...book.While very curious to see
While learning Sitecore I have found that the majority of Sitecore sample code on
While learning to use LINQ in VB.NET, I came across the following: Dim x
I am new to ASP.Net and I'm a little confused here. While learning ASP.Net
I started learning ASP.NET MVC3. So, while reading tutorials online and in books, I

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.