Re: == versus .Equals()

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

From: Anders Norås [MCAD] (anders.noras_at_objectware.no)
Date: 12/29/04


Date: Wed, 29 Dec 2004 13:26:34 +0100


> This is something that has been bugging me for some time. When exactly
> should I be using == instead of .Equals() ?

The == operator uses the static method op_Equality while Equals is an
instance method inherited from Object. Since the methods are members of each
class, or inherited from a base class, the implementation will vary from
class to class. However, in most scenarios you should get the same result if
whether you use the == operator or the Equals method.
To explain the differences I will use the String class as an example.
The String class inherits the instance method Equals from System.Object, has
an overloaded == operator and has its own static Equals method. The String
class can have a static method with the same name as the instance method
Equals inherited from Object since the methods have different signatures.

When using the == operator, the String class uses the static Equals method
internally. This method first uses the == operator to check if the strings
to compare are the same object. This is equal to using the
Object.ReferenceEquals mehtod.
If neither of the strings to compare are null, the static Equals method
calls the instance method Eqauls on the string to the left of the operator
and passes the string to the right of the operator as a parameter to perform
a string compare.
The C# compiler ensures that all strings with the same content all point to
the same object. Both s1, s2 and s3 in the following example point to the
same physical string object in memory, and hence have reference equality.
string s1="abc";
string s2="abc";
string s2=new string(new char[] {'a','b','c'});

When string comparsions the == operator is slower than the Equals method
when the strings are the same. If the strings are different the Equals
method will be faster. However, string comparison in .NET is super fast, so
in a real world application this won't make a difference.

Using the Equals operator determines whether two Object instances are the
same. The == operator determines whether two Objects have the same value.
Keep in mind that user-defined types can overload the == operator and the
Equals method alter their behavior.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/



Relevant Pages

  • Re: Function that returns date of file.
    ... string after the date/time when it is used by itself. ... Is that your entire script? ... I make an IF statement that required the 'equals equals'. ... designed database your job will be all that much harder. ...
    (alt.php)
  • Re: Catching invalid string comparisons
    ... the programmer expected it to check if it is equal to an empty string. ... compares for equality -- but two references are equal ... Comparing the objects themselves, ... requires use of the equals() method. ...
    (comp.lang.java.programmer)
  • Re: hashCode() for Custom classes
    ... I am currently faced with the task of providing a logical equals() ... have to override the hashCode() so that when an object of this class ... String name; ... The hash code of Agent is the combined hash code of ...
    (comp.lang.java.programmer)
  • Re: Compare Two Structure Data Types...
    ... I normally override & overload the Equals method on the Structure to do the ... If I overloaded Equals in A for B, I would also overload Equals in B for A, ... > Public LastName as String ... > Public City as String ...
    (microsoft.public.dotnet.languages.vb)