= Compare the content in the stack, that is, compare the value type or compare the reference.
The value type is compared with "=", that is, the Compare value. the compare of the reference type is to compare whether the addresses in the two reference types are the same in the stack.
Equals () is divided into two types: Virtual method and static method. If the comparison is reference, the same as referenceequals (),
However, the virtual method of equals () is often override to compare the actual value of the reference type. This is its most important purpose, for example. net's string class overwrites the equals () method, which is used to compare whether the values of two strings are equal, rather than whether the references are equal. If the value type is compared using the equals () method, the return value must be false, because the packing operation must be performed first.
MS reference Chinese version: http://msdn.microsoft.com/zh-cn/library/ms173147.aspx
the referenceequals, equals and operator = are described as follows (partial excerpt)
there are two differences in C #: equal references and equal values. equality means that two objects contain the same value. For example, two integers with a value of 2 have equal values. Referencing equal means that not two objects are to be compared, but two objects are referenced, and the two references the same object. (In C #, there are two different kinds of equality: Reference equality (also known as identity) and value equality. value Equality is the generally understood meaning of equality: it means that two objects contain the same values. for example, two integers with the value of 2 have value equality. reference equality means that there are not two objects to compare. instead, there are two object references and both of them refer to the same object .)
to check the equality of reference, use referenceequals. To check the equality of values, use equals. (to check for reference equality, use referenceequals. to check for Value Equality, you shocould generally use equals .)
override equals
since equals is a virtual method, any class can override its implementation. A value (essentially any value type) or any class of a set of values (such as the plural class) should override equals. (Because equals is a virtual method, any class can override its implementation. any class that represents a value, essential any value type, or a set of values as a group, such as a complex number class, shocould override equals .)
rewrite operator ==
by default, operator = checks whether two references indicate the same object to test whether the references are equal . Therefore, you do not need to implement the = Operator to obtain the reference type. When the type is immutable (that is, the data contained in the instance cannot be changed), it may be useful to compare whether the value is equal by using the overload operator =, rather than comparing whether the reference is equal, as an unchangeable object, as long as its value is the same, it can be treated as the same. we recommend that you do not overwrite the = Operator in a non-Immutable type . ( by default, the operator = tests for reference equality by determining whether two references indicate the same object. therefore, reference types do not have to implement operator = in order to gain this functionality. when a type is immutable, that is, the data that is contained in the instance cannot be changed, overloading operator = to compare value between ity instead of reference between ity can be useful because, as immutable objects, they can be considered the same as long as they have the same value. it is not a good idea to override operator = in non-Immutable types. )
The system. string class object cannot be changed after initialization. It is a good example of the application = specification. But not all. Net class libraries comply with this specification. For example, in system. drawing. point (similar to point, rectangle, etc), point is not an immutable type (point. offset changes the status of the initialized Point Object), but the class is overloaded with = and used for value comparison. Of course, I agree that the point implementer is in line with the programming habits in C ++, but it is not consistent with the specifications after all.
-Ms has clear rules for referenceequals and equals (orange reference text ). No. Net class library inconsistent with the specification is found.
-For operator =, we can see from the specification that the attitude is ambiguous. On the one hand, Ms tends to reference equal values (see the previous blue field) to be similar to Java's = specification. Maybe it's because. the main designer of the Net Language used to be the main designer of Java, or it may be to take care of J # users). On the other hand, to be compatible with the = heavy load habits of C ++, for some value types, you still want to use the operator = as the operator with equal values. (The Red field in the previous section) leads to confusion during use (including. Net class library implementers.