问题
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?
回答1:
As far 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.
Edit:
Note that the ==
operator is a static method. It is bound at compile time, base on the types of your variables or fields. Equals
is a virtual method that is found at runtime, based on actual runtime types.
回答2:
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;
}
回答3:
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.
回答4:
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")
回答5:
What it does depends on the context.
http://en.csharp-online.net/ECMA-334:_14.9_Relational_and_type-testing_operators
回答6:
The behavior of == operator depends how the variable you are applying it to was declared (not on the class of the object, I'll add an example).
For value types it will compare their values.
For reference types a == b returns true if a is the same object as b, unless the == operator was overloaded. Not overridden as others said, you can't override operators in c# because they are not virtual.
object obj_a, obj_b;
string str_a, str_b;
str_a = "ABC";
str_b = new string("ABC".ToCharArray());
obj_a = str_a;
obj_b = str_b;
Console.WriteLine("str_a == str_b = {0}", str_a == str_b); // in string == operator is overloaded
Console.WriteLine("str_a.Equals(str_b) = {0}", str_a.Equals(str_b)); // string overrides Object.Euqals
Console.WriteLine("obj_a == obj_b = {0}", obj_a == obj_b); // in object == operator is not overloaded
Console.WriteLine("obj_a.Equals(obj_b) = {0}", obj_a.Equals(obj_b)); // Object.Equesl is virtual and overridden method from string will be executed.
Console.ReadKey();
The output of that program is
str_a == str_b = True str_a.Equals(str_b) = True obj_a == obj_b = False obj_a.Equals(obj_b) = True
来源:https://stackoverflow.com/questions/806020/c-sharp-what-does-the-operator-do-in-detail