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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:45:21+00:00 2026-05-31T18:45:21+00:00

The VS2005 documentation Guidelines for Overloading Equals() and Operator == (C# Programming Guide) states

  • 0

The VS2005 documentation Guidelines for Overloading Equals() and Operator == (C# Programming Guide) states in part

Overriding operator == in non-immutable types is not recommended.

The newer .NET Framework 4 documentation Guidelines for Implementing Equals and the Equality Operator (==) omits that statement, although one post in Community Content repeats the assertion and references the older documentation.

It seems that it is reasonable to override Equals() at least for some trivial mutable classes, such as

public class ImaginaryNumber
{
    public double RealPart { get; set; }
    public double ImaginaryPart { get; set; }
}

In math, two imaginary numbers that have the same real part and the same imaginary part are in fact equal at the point in time that equality is tested. It is incorrect to assert that they are not equal, which would happen if separate objects with the same RealPart and ImaginaryPart were Equals() not overridden.

On the other hand, if one overrides Equals() one should also override GetHashCode(). If an ImaginaryNumber that overrides Equals() and GetHashCode() is placed in a HashSet, and a mutable instance changes its value, that object would no longer be found in the HashSet.

Was MSDN incorrect to remove the guideline about not overriding Equals() and operator== for non-immutable types?

Is it reasonable to override Equals() for mutable types where “in the real world” equivalence of all properties means that the objects themselves are equal (as with ImaginaryNumber)?

If it is reasonable, how does one best deal with potential mutability while an object instance is participating in a HashSet or something else that relies on GetHashCode() not changing?

UPDATE

Just came across this in MSDN

Typically, you implement value equality when objects of the type are
expected to be added to a collection of some sort, or when their
primary purpose is to store a set of fields or properties. You can
base your definition of value equality on a comparison of all the
fields and properties in the type, or you can base the definition on a
subset. But in either case, and in both classes and structs, your
implementation should follow the five guarantees of equivalence:

  • 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-31T18:45:23+00:00Added an answer on May 31, 2026 at 6:45 pm

    I came to realize that I wanted Equals to mean two different things, depending on the context. After weighing the input here as well as here, I have settled on the following for my particular situation:

    I’m not overriding Equals() and GetHashCode(), but rather preserving the common but by no means ubiquitous convention that Equals() means identity equality for classes, and that Equals() means value equality for structs. The largest driver of this decision is the behavior of objects in hashed collections (Dictionary<T,U>, HashSet<T>, …) if I stray from this convention.

    That decision left me still missing the concept of value equality (as discussed on MSDN)

    When you define a class or struct, you decide whether it makes sense
    to create a custom definition of value equality (or equivalence) for
    the type. Typically, you implement value equality when objects of the
    type are expected to be added to a collection of some sort, or when
    their primary purpose is to store a set of fields or properties.

    A typical case for desiring the concept of value equality (or as I’m terming it “equivalence”) is in unit tests.

    Given

    public class A
    {
        int P1 { get; set; }
        int P2 { get; set; }
    }
    
    [TestMethod()]
    public void ATest()
    {
        A expected = new A() {42, 99};
        A actual = SomeMethodThatReturnsAnA();
        Assert.AreEqual(expected, actual);
    }
    

    the test will fail because Equals() is testing reference equality.

    The unit test certainly could be modified to test each property individually, but that moves the concept of equivalence out of the class into the test code for the class.

    To keep that knowledge encapsulated in the class, and to provide a consistent framework for testing equivalence, I defined an interface that my objects implement

    public interface IEquivalence<T>
    {
        bool IsEquivalentTo(T other);
    }
    

    the implementation typically follows this pattern:

    public bool IsEquivalentTo(A other)
    {
        if (object.ReferenceEquals(this, other)) return true;
    
        if (other == null) return false;
    
        bool baseEquivalent = base.IsEquivalentTo((SBase)other);
    
        return (baseEquivalent && this.P1 == other.P1 && this.P2 == other.P2);
    }
    

    Certainly, if I had enough classes with enough properties, I could write a helper that builds an expression tree via reflection to implement IsEquivalentTo().

    Finally, I implemented an extension method that tests the equivalence of two IEnumerable<T>:

    static public bool IsEquivalentTo<T>
        (this IEnumerable<T> first, IEnumerable<T> second)
    

    If T implements IEquivalence<T> that interface is used, otherwise Equals() is used, to compare elements of the sequence. Allowing the fallback to Equals() lets it work e.g. with ObservableCollection<string> in addition to my business objects.

    Now, the assertion in my unit test is

    Assert.IsTrue(expected.IsEquivalentTo(actual));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside VS2005, our whole programming staff gets this error message sporadically and it is
I use VS2005 but the version is not integrated to fxcop. Now I need
I have a web application on VS2005 using C#. Whenever my webpage does not
The VS2008 SP1 documentation talks about std::tr1::mem_fun . So why, when I try and
I have a VS2008 solution using xml documentation, and we have warnings as errors
vs2005 support ::stdext::hash_map ::std::map. however it seems ::stdext::hash_map's insert and remove OP is slower
In VS2005 and up, is it possible to specify which configuration should be selected
Using VS2005/2008 as a resource editor, one of the options in the Add Resource
I'm currently using VS2005 Profesional and .NET 2.0, and since our project is rather
I recently upgraded a VS2005 web deployment project to VS2008 - and now 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.