views:

846

answers:

6

in c# what does exactly happen in the background when you do a comparison with the "==" operator on two objects? does it just compare the addresses? or does it something like Equals() or CompareTo() ?

PS: what about the "==" operator in java? does it behave the same?

+22  A: 

As I know:

  • it compares value types by value (equality)
  • it compares reference types by reference (identity)
  • except if the == operator is overloaded, then it calls that one.

Equals is implemented in object and can be overridden as well. The default implementation in Object performs a reference comparison for reference types. So by default, Equals and == do the same.

I think in java you cannot overload the == operator. But my Java knowledge is pretty outdated.

Stefan Steinegger
Excellent answer; at least one omission, though: http://stackoverflow.com/questions/806020/snip/806068#806068
Marc Gravell
you can not override == operator in java, for reference types in java == will always do a reference(identity) comparision.
David Waters
You can't override operators in C# because they are not virtual. You can only overload them.
Michał Piaskowski
@Michal: Thanks, your obviously right, the operators are even static. I fixed the terms.
Stefan Steinegger
+1  A: 

What it does depends on the context.

http://en.csharp-online.net/ECMA-334:_14.9_Relational_and_type-testing_operators

Lucero
+4  A: 

From MSDN:

For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object. For the string type, == compares the values of the strings.

kitchen
+3  A: 

No ... the == operator does not always behave the same in java and in c#.

For example with Strings; Java == does compare the references of the string objects... (if you use primitve types, == in java compares the values). That's why

// returns FALSE in JAVA
(new String("test") == "test")

will not return true in java...

In C# in contrast, the == operator does behave different on strings. For example, it will return true in the following case:

// returns TRUE in C#
(new String("test".ToCharArray()) == "test")
Homes2001
This is because the == operator can not be overridden in Java, but in C#, and that's what it is for strings. Does this make the operator behaviour different?
Stefan Steinegger
I think that's a common pitfall when you are used to write software in C# and then use Java in another project... that's why I wanted to point it out
Homes2001
+8  A: 

As an extension to Stefan's excellent answer - another exception is if the operands involve Nullable<T> - in which case "lifted" operators apply (14.2.7 in ECMA 334v4):

For the equality operators == !=

a lifted form of an operator exists if the operand types are both non-nullable value types and if the result type is bool. The lifted form is constructed by adding a single ? modifier to each operand type. The lifted operator considers two null values equal, and a null value unequal to any non-null value. If both operands are non-null, the lifted operator unwraps the operands and applies the underlying operator to produce the bool result.

What that means is: because there is an equality operator between (say):

int i = ..., j = ...;
bool eq = i == j;

Thus there is an implicit operator of the form (although done differently):

int? i = ..., j = ...;
bool eq;
if(i.HasValue) {
    if(j.HasValue) { // both have values; compare
       eq = i.GetValueOrDefault() == j.GetValueOrDefault();
    } else { // one null; always false
       eq = false;
    }
} else { // true if both null, else false
    eq = !j.HasValue;
}
Marc Gravell
A: 
Michał Piaskowski