By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,152 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,152 IT Pros & Developers. It's quick & easy.

== versus .Equals()

P: n/a
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?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Chris wrote:
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

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x0270466B
Nov 16 '05 #2

P: n/a
> 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/
Nov 16 '05 #3

P: n/a
"Chris" wrote:
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
Nov 16 '05 #4

P: n/a
Chris <no****@devdex.com> wrote:
This is something that has been bugging me for some time. When exactly
should I be using == instead of .Equals() ?


See http://www.pobox.com/~skeet/csharp/faq/#equals

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.