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

  • Home
  • SEARCH
  • 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
  • 3 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

I've always wondered about how and what the best way to go about implementing
I wondered if there are any alternatives to Scala that attempt to offer a
I wondered if Java7's new invokedynamic bytecode instruction could be used to implement multiple
In this post I wondered about cleaner code when internationalising an app. that leads
This is something I've always wondered about. I understand that horizontal scaling is about
Had a question that I've often wondered about. Is it better to have multiple
I just wondered what the best way is to get rid of the globally
This is something I've wondered about in a couple of frameworks that I've messed
One thing I've always wondered about is if the instances of std::string that I
This is something that I have always wondered about, but never bothered to profile.

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.