c# How does an == decide if two objects are the same?

Hey guys,

I’ve had a bit of a google around for the answer to this question, maybe I don’t know the terms I need in order to find the answer so I’m going to ask on here. Please forgive me if this has been answered somewhere before.

I know that most things are passed around by reference in unity [source] so I wonder if it is the memory address that is compared; that way it would return true if the two references referred to the same instance of a class. Perhaps (as I do) you might want to compare the values of an object, would comparing two instances of a class return true if they were different instances but had the same values?

I realise I could test both of these cases but I’m interested in the logic that lies behind a comparison so I can make a more informed decision about comparisons when I’m coding.

Thanks in advance for any help you can provide me.

The Object class has a virtual method called Equals.

BY default, that method calls a non-virtual method called ReferenceEquals.
You can read about ReferenceEquals here but yes, basically, it checks the location in memory.

HOWEVER any class can override Equals and many do. So you need to check the documentation for the class in question.