When do you need to override Equals () in a .NET class?

.NET contains its own equality comparison functions, however I really don't understand how this works.

If the desired behavior of Equals () is ==to verify that each field of an object is equal to each field of another object, is it necessary to override Equals () using a method that does this explicitly?

+3
source share
2 answers

If you are working with a class, then yes, this is necessary.

With reference types (classes), .NET provides an equality comparison by default, which compares the link itself, not the values ​​inside the class. If you want to compare field by field, you must redefine equals.

structs ( ) .

:

Equals . , , . , .

+5

.

- ReferenceEquality: , . -, , .

, String . 2 . , String - , .

+1

Source: https://habr.com/ru/post/1748940/


All Articles