D
D
ddd3292021-02-11 19:35:54
.NET
ddd329, 2021-02-11 19:35:54

Is the Equals method overridden correctly in C#?

The Microsoft site provides an example of how to override the Equals method.
6026a0990639e880298883.png
Confusingly, there is no this.GetType() != other.GetType() check, as is usually given in examples.
Do you think this is correct?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-02-11
@ddd329

In this case, the implementation is correct, but it may behave incorrectly if you create a descendant of ImaginaryNumber.
So in a good way it is necessary to check the types through GetType, which you noticed.
GetHashCode, by the way, is now recommended to be implemented through the HashCode structure

V
Vladimir Korotenko, 2021-02-11
@firedragon

When it is cast from an object there and the type is checked

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question