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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:47:30+00:00 2026-05-11T03:47:30+00:00

I wondered about the best way to implement a correct, flexible and fast Equals

  • 0

I wondered about the best way to implement a correct, flexible and fast Equals in C#, that can be used for practically any class and situation. I figured that a specialized Equals (taking an object of the actual class as parameter) is needed for performance. To avoid code duplication, the general Equals ought to call the specialized Equals. Null checks should be performed only once, even in inherited classes.

I finally came up with this design:

 class MyClass {     public Int32 SomeValue1 = 1;     public Int32 SomeValue2 = 25;      // Ignoring GetHashCode for simplicity.      public override bool Equals(object obj)     {         return Equals (obj as MyClass);     }      public bool Equals(MyClass obj)     {         if (obj == null) {             return false;         }          if (!SomeValue1.Equals (obj.SomeValue1)) {             return false;         }          if (!SomeValue2.Equals (obj.SomeValue2)) {             return false;         }          return true;     } }  class MyDerivedClass : MyClass {     public Int32 YetAnotherValue = 2;      public override bool Equals(object obj)     {         return Equals (obj as MyDerivedClass);     }      public bool Equals(MyDerivedClass obj)     {         if (!base.Equals (obj)) {             return false;         }          if (!YetAnotherValue.Equals (obj.YetAnotherValue)) {             return false;         }          return true;     } } 

Important ideas:

  • Usage of the ‘as’ operator. This way we don’t have to check for nulls in the general Equals. Wrong class types get reduced to null and will be sorted out in the specialized Equals.
  • Null check at exactly one point, even for derived classes.
  • Checking the attributes one after another provides a clear structure.

Are there flaws in this concepts, or did i miss any conditions?

  • 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. 2026-05-11T03:47:31+00:00Added an answer on May 11, 2026 at 3:47 am

    Your Equals method isn’t reflexive when different types are involved:

    MyDerivedClass mdc = new MyDerivedClass(); MyClass mc = new MyClass(); Object omdc = mdc; Object omc = mc;  // mc.Equals(mdc) - true // mdc.Equals(mc) - true by calling the right overload // omc.Equals(omdc) - true // omdc.Equals(omc) - false, the 'as' in MyDerivedClass will result in null 

    The normal way round this is to use:

    if (GetType() != other.GetType()) {     return false; } 

    See the docs in Object.Equals: ‘x.Equals(y) returns the same value as y.Equals(x).’ Relying on overloading to give different results could end up with horribly issues which would be very subtle to debug.

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

Sidebar

Related Questions

What is the best way to handle an observable array which can be committed/thrown
I've just validated a new site a new site that i've written using Rails
I've always wondered if it would be possible to show UTF8 or UTF16-Chinese text
I sometimes get frustrated by the lack of type safety with j.u.Calendar and wondered
I have a .NET app that I'd like to do some automated testing on.
Does anyone know of a clean way to simulate lightning and thunder mainly in
I'm looking to construct an object from lots of smaller boxes and joins in
I am having a problem with a Many-to-Many association and the session in nHibernate
Since graphics cards provide large amounts of RAM (0.5GiB to 2GiB) and API access
I made the error of running what should have been a quick update against

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.