How does the == operator work in C #? If it is used to compare objects of class A, will it try to match all properties of A or will it look for pointers to the same memory cell (or, possibly, something else)?
Let me create a hypothetical example. I am writing an application that uses the Twitter API and has a Tweet class that has all the properties of a single tweet: text, sender, date and time, source, etc. If I want to compare objects of the Tweet class for equivalence, can I just use:
Tweet a, b; if (a == b) { //do something... }
Will it check the equivalence of all properties of the Tweet class between a and b?
If not, would it be the right approach to overload the == operator to explicitly check the equivalence of all fields?
UPDATE:. Of the first two answers, I'm right in assuming:
- If the
== operator or the Equals method is not overloaded for the class, the == operator is used for the class of the object. - The
== operator for an object class checks equality in a memory cell. - I need to overload the
== operator or the Equals method to complete this task. - In overloading, I need to check the equivalence of properties manually, so there is no way to do this semi-automatically, say, in a loop , right?
UPDATE # 2: Yuri made a comment that you can check the equivalence in the properties of the == operator with reflection . How can this be done? Could you give me some sample code? Thanks!
source share