tags:

views:

1994

answers:

6

So, when I was a comparitive novice to the novice I am right now, I used to think that these two things were syntactic sugar for each other, ie. that using one over the other was simply a personal preference. Over time, I'm come to find that these two are not the same thing, even in a default implementation (see this and this). To further confuse the matter, each can be overriden/overloaded seperately to have completely different meanings.

Is this a good thing, what are the differences, and when/why should you use one over the other?

+6  A: 

http://msdn.microsoft.com/en-us/library/ms173147.aspx
has some good info on the differences

Kevin Sheffield
+1  A: 

My understanding of the uses of both was this: use == for conceptual equality (in context, do these two arguments mean the same thing?), and .Equals for concrete equality (are these two arguments in actual fact the exact same object?).

Edit: Kevin Sheffield's linked article does a better job of explaining value vs. reference equality…

andrewdotnich
Agreed, but that does sound like a good rule of thumb.
Patrick Szalapski
+12  A: 

MSDN has clear and solid descriptions of both things.

object.Equals method

operator ==

Overloadable Operators

Guidelines for Overriding Equals() and Operator ==

Is this a good thing, what are the differences, and when/why should you use one over the other?

How can it be "good" or "bad" thing? One - method, another - operator. If reference equality is not sufficient, overload them, otherwise leave them as is. For primitive types they just work out of box.

aku
As someone getting into generics the differences can be vast when you are calling them on any type T blindly.
Matthew Scharley
"blindly" is a bad practice for anything. if you know the answer on your question, why asking?
aku
Even if I did know a concrete answer (which I don't), perhaps for the same reason people ask questions and answer themself? Also, how can you do anything else for a generic type T? If you start doing things like if (typeof(T) == typeof(int)), what's the point?
Matthew Scharley
As for my indignation, I encountered to many situations when I provide throughout answer, but person just want to show off and repeat my answer with some minor additions, thus wasting my time. If you really don't know the answer, I apologize.
aku
"Also, how can you do anything else" Sorry but I don't quite understand what you mean. Maybe you can ask concrete question about generics?
aku
Given say List<T>, List shouldn't care what type T is, it shouldn't check anywhere what sort of object T is, it should only use Object's methods and expected results (excluding when there's restrictions on what T is). Or am I wrong here?
Matthew Scharley
List<Apple> and List<Orange> are two completely different classes. Each type can behave differently. In each case you should read description of Equal and == methods to understand how it works. If there are no notices, consider that only reference equality is used.
aku
If you want to discuss implementation of equality for generic collections, create new question. Comments is not a place for new questions.
aku
A: 

You may want to use .Equals as someone may come along at a later time and overload them for you class.

Ed Swangren
But wouldn't that be a good thing?
Patrick Szalapski
Yeah, I think I meant to say it the other way around.
Ed Swangren
+7  A: 
string x = "hello";
string y = String.Copy(x);
string z = "hello";

To test if x points to the same object as y:

(object)x == (object)y  // false
x.ReferenceEquals(y)    // false
x.ReferenceEquals(z)    // true (because x and z are both constants they
                        //       will point to the same location in memory)

To test if x has the same string value as y:

x == y        // true
x == z        // true
x.Equals(y)   // true
y == "hello"  // true

Note that this is different to Java. In Java the == operator is not overloaded so a common mistake is:

y == "hello"  // false (y is not the same object as "hello")

For string comparison in Java you need to always use .equals()

y.equals("hello")  // true
molasses
A: 

Two of the most often used types, String and Int32, implement both operator==() and Equals() as value equality (instead of reference equality). I think one can consider these two defining examples, so my conclusion is that both have identical meanings. If Microsoft states otherwise, I think they are intentionally causing confusion.

Dimitri C.