== versus .Equals()

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

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

I see a lot of code that performs object evaluation e.g. Is Object1 the
same as Object2 by using == which works but shouldn't it be using
.Equals()? e.g. Object1.Equals(Object2)

I believe == should be using for simple type evaluation e.g.

int a = 0;
a++;

if (a == 1) {
... do something
}

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

I see a lot of code that performs object evaluation e.g. Is Object1 the
same as Object2 by using == which works but shouldn't it be using
Equals()? e.g. Object1.Equals(Object2)

I believe == should be using for simple type evaluation e.g.

int a = 0;
a++;

if (a == 1) {
... do something
}

Any opinions?

== is implemented as an operator which is a static method call. In your
case, Object1.Equals(Object2) you're depending on Object1 being a live
object, if it's null you're getting an exception.

Here's the rules I use:

- for all built-in types, use == for value comparison,
Object.ReferenceEquals for reference comparison
- for all other types, I check if they have a == operator and use that
if they do, otherwise I use .Equals but then I also make sure to check
against null
 
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/
 
Chris said:
This is something that has been bugging me for some time. When exactly
should I be using == instead of .Equals() ?

The fundamental rule is to make sure the thing you choose does what you
want/expect. This usually involves reading the documentation ;-)

Everything has an Equals method (since it comes from Object).

For a reference type (class/array), the default behavior of Equals is
reference equality. Classes can override it to provide value equality if they
want to - the docs for a class should tell you the behavior.

For a value type (struct/enum), the default behavior of Equals is value
equality (pairwise comparison of all the fields using their Equals method).

Not everything has an == operator (all classes do but a struct may or may
not have one).

The default behavior of == on a class is the same as the default behavior of
the Equals method for a class; that is, reference equality. The class
designer can overload the operator to perform value equality if they would
like to. Check the docs to see the behavior.

structs do not get an == operator for free. If you try to use == and the
struct designer did not overload it, your code won't compile. Of course, the
type designer can overload the operator to do whatever they want.

So I think it comes down to reading the docs and personal preference.

Mark
 
Back
Top