RE: Equality vs Sameness

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Good call on my misuse of overriding vs overloading.

And I agree about overloading == for strings.  The MS guidelines for 
overloading operators do suggest overloading == for strings and other 
reference types that act like value types.  So maybe "any" was too strong of 
a word.  But between you and me, I can't think of a reason to overload == for 
a string.  I guess it just hasn't come up in my experience.

So, *grin*. let's heat things up and get to the last item... Overriding 
Equals and GetHashCode.

I agree about not writing code that hasn't been asked for.  Developers 
shouldn't add operations to a class because they think the user would benefit 
from it in the future if the users haven't asked for that functionality.  But 
while that applies to business logic, there are basic functionality expected 
in objects that I always include.  

For instance, I never create a CollectionBase based class and leave out 
IndexOf or Contains just because when I create the class I don't know if it 
will be used.  It is a collection and all developers on the team will expect 
the collections I create to have that functionality without having to put in 
a change request to get it.

I am the same way about Equals.  If you have a custom collection for your 
class, IndexOf, Contains, and many other methods require value equality, not 
referential equality, in order to work.  If your class implements IComparable 
so you can sort it, you have to override Equals as well.

It's just my opinion but, to me, overriding Equals is a good practice and 
time well spent in any class except the most trivial or those with very 
specific functional limitations that would make them exceptions


-- 
Dale Preston
MCAD C#
MCSE, MCDBA


"Jon Skeet [C# MVP]" wrote:

> Dale <dale0973@xxxxxxxxxxxxx> wrote:
> > Let me add one more piece to the Equals puzzle.  By default, Object.Equals() 
> > tests referential equality and, therefore, does the same thing as 
> > ReferenceEquals.  The difference is that Equals is a virtual method and can, 
> > and IMO usually should, be overridden in your object class.
> 
> Usually should? Very few of the classes I write will ever be checked 
> for equality. The "You Ain't Gonna Need It" principle of agile 
> development suggests that it's not worth overriding Equals until you 
> actually need it to be overridden - which has saved me a lot of code...
> 
> (That's especially true as you should override GetHashCode when you 
> override Equals...)
> 
> > The == operator, on the other hand, can but should not be overridden in any 
> > reference types.  It is acceptable to override the == operator in value types.
> 
> So do you believe that == shouldn't have been overloaded (not
> overridden - you can't override operators) for System.String?
> 
> For immutable reference types which are effectively acting as "value 
> objects", I see nothing wrong with overloading ==. It can make code a 
> lot more readable.
> 
> -- 
> Jon Skeet - <skeet@xxxxxxxxx>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
> 
.



Relevant Pages

  • Re: Why this overloading example works this way?
    ... have to think that equals() and hashcodeare intimatelly related, ... overloading fixes the actual method to call at ... Whenever I override the equals method, ... public boolean equals{ ...
    (comp.lang.java.programmer)
  • RE: Equality vs Sameness
    ... > Good call on my misuse of overriding vs overloading. ... So you override Equals and GetHashCode even for forms, ... or wanted to compare two instances of a singleton? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Can someone explain this to me?
    ... references for reference types ... But the dotnet way of comparing objects for new class types is to override ... If you call Equals, yes. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Class design ToString
    ... Overriding methods is different from overloading operators, you can override ... > public override string ToString() ... >>> public overloads function Tostring as string ...
    (microsoft.public.dotnet.general)
  • Re: Can someone explain this to me?
    ... references for reference types ... But the dotnet way of comparing objects for new class types is to override ... So why doesn't == use Equals? ...
    (microsoft.public.dotnet.languages.csharp)