Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default behavior of Equals Method?

Tags:

c#

Let A be a class with some members as x, y, z:

Class A {   int x;   int y;   String z;   ... } 

A is an Object so it inherits the "Equals" functions defined in Object. What is the default behavior of this function? Does it check for the equality of members or does it check for reference equality?

like image 733
Mostafa Mahdieh Avatar asked Apr 13 '10 18:04

Mostafa Mahdieh


People also ask

What is the default equals method?

equals() method compares the object references or the memory location where the objects are stored in the heap. Thus by default the . equals() method checks the object by using the “==” operator.

What is default Behaviour of equals method in Java?

The equals method checks by default whether the object given as a parameter has the same reference as the object it is being compared to. In other words, the default behaviour checks whether the two objects are the same. If the reference is the same, the method returns true , and false otherwise.

What is the default behavior of equal method of class Object?

equals(Object otherObject) – verifies the equality of two objects. Its default implementation simply checks the object references of two objects to verify their equality. By default, two objects are equal if and only if they are refer to the same memory location.

What is the default implementation of equals method C#?

The default implementation calls Object. Equals(Object) on each field of the current instance and obj and returns true if all fields are equal. Particularly if your value type contains fields that are reference types, you should override the Equals(Object) method.


2 Answers

The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation.

http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx

like image 113
Greg Avatar answered Sep 20 '22 21:09

Greg


it checks for reference unless you override equals

like image 29
UshaP Avatar answered Sep 20 '22 21:09

UshaP