D
D
Denis Bredun2021-08-11 00:45:17
C++ / C#
Denis Bredun, 2021-08-11 00:45:17

Am I understanding correctly how Equals, ReferenceEquals and == work?

Private types and possibly arrays:
For reference types, unless overridden:
Equals: compares references, but not properties\fields if they are the same
ReferenceEquals: compares references
==: compares references
Primitive types:
Meaningful:
Equals: compares values ​​considering of type
ReferenceEquals: references are compared
==: values ​​are compared
Reference:
Equals: values ​​are compared taking into account type
ReferenceEquals: references are compared
==: values ​​are compared taking into account type

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Griboks, 2021-08-11
@Luffy1

To understand correctly, you need to read the official documentation.

V
Vasily Bannikov, 2021-08-11
@vabka

No, not right.
Equals - the Equals method is used, which by default compares references, but it is often overridden.
ReferenceEquals - references are compared
== - an operator is called, which is not defined by default in principle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question