C # various MSDN tutorials on implementing Equals

Why do the following documents discuss different approaches to implementing the method Equals?

The second document (later) does not explicitly implement a strongly typed version Equals(for example, public bool Equals(MySuperTrooperClass o)).

What is the main reason to abandon a strictly typed method from one of the recommendations and what approach should I use in my production code?

+3
source share
4 answers

It is not possible to refuse a strongly typed version. On the contrary, since the first page itself mentions

, Equals (), Equals (type) , .

.

, , System.Object . IEquatable<T>, , Equals(T) Equals(object).

+3

Microsoft Equals , , ((Object)X).Equals(Y) ( ), - , X.Equals(Y) . , 3.Equals(3.0) false, (3.0).Equals(3) true. == (, X==Y, Y==X), - (, X = Int64.MaxValue, Y = X-1 Z = (Double) X, X == Z Y == Z, X!= Y).

, , Microsoft Equals(), Equals(Object), , , , , - .

+1

Equals .

0

, , == , , Equals().

, Equals() , == .

As for GetHashCode () - in some cases, structures such as a dictionary use this particular method to distinguish between data. You decide how your data should be distinguished, but GetHashCode () should not be changed until the values ​​of the object that will be changed to work correctly. This means that it must maintain the same value during the life of the object.

When you do this:

MyClass A = new MyClass();
MyClass B = A;

When you check to see if (A == B), you really check to see if the address of pointer A has the same value as the address of pointer B. Thus, you do not need to implement Equals () in 99.99% of the total development.

0
source

All Articles