Re: Compare two instance of a class

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

From: richlm (rich_lm_at_h0tmai1.com)
Date: 08/02/04


Date: Mon, 2 Aug 2004 11:23:54 +0200

Recommended practice is that operator "==" and method
"Object.Equals(object)" should behave the same.
If you override Equal you should also override GetHashCode.

You need to bear in mind whether or not you will ever need to compare 2
references (which is the default behaviour) i.e. that refer to the same
instance.

In most cases I think it is better to implement a specific method to compare
reference type instances - value comparison is often a business/application
specific function, and it may be misleading to some developers if you
override default language functionality.

Here's a useful article on MSDN:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconimplementingequalsoperator.asp

Richard.

"bengamin" <saixh@langchao.com> wrote in message
news:eayVLUDeEHA.3476@tk2msftngp13.phx.gbl...
> Hi,
>
> I have a C# class and two instance of the class;
>
> the class have some property.
>
> I want to compare the property value of the two instance
>
> How should i do? override == ? use delegate ?
>
> I am sorry ,my english was poor.
>
>
>



Relevant Pages

  • Re: Compare two instance of a class
    ... // A Person class, that have: FirstName, LastName, Age, Gender. ... >>If you override Equal you should also override GetHashCode. ... >>You need to bear in mind whether or not you will ever need to compare 2 ... >>override default language functionality. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Compare Two Identical Datatable By Content
    ... Even if you override the operators the ... public static bool operator!= ... public override bool Equals(object obj) ... compare the schema. ...
    (microsoft.public.dotnet.languages.csharp)
  • 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: Why use Objectg in Equals method?
    ... superclasses (but not subclasses). ... Of course, if you intend to compare with objects of different classes, ... It is convenient that 'equals(Object o)' is an override - it allows for expressive compactness in a ton of algorithms, e.g., those on which collections classes rely. ...
    (comp.lang.java.programmer)
  • Re: Compare two instance of a class
    ... Sorry but I've gotten curious because of these replies. ... > If you override Equal you should also override GetHashCode. ... > You need to bear in mind whether or not you will ever need to compare 2 ... > override default language functionality. ...
    (microsoft.public.dotnet.languages.csharp)