As I know, everything derived from object except interfaces in .net. But I noticed that when I press "." after interface name Equals method appears. And when I press F12 for equals method, it directs to equals method in object class. If interfaces are not derived from object class, where is equals method coming from?
Definition and Usage. The equals() method compares two strings, and returns true if the strings are equal, and false if not.
We can override the equals method in our class to check whether two objects have same data or not.
Java hashCode() An object hash code value can change in multiple executions of the same application. If two objects are equal according to equals() method, then their hash code must be same. If two objects are unequal according to equals() method, their hash code are not required to be different.
The equals() method is a static method of the Objects class that accepts two objects and checks if the objects are equal. If both the objects point to null , then equals() returns true .
From section 13.2 of the C# 4 spec:
Note that the members in class object are not, strictly speaking, members of any interface (§13.2). However, the members in class object are available via member lookup in any interface type (§7.4).
And section 7.4:
- First, a set of accessible members named N is determined:
- ...
- Otherwise, the set consists of all accessible (§3.5) members named N in T, including inherited members and the accessible members named N in object. [...]
And section 7.4.1:
For purposes of member lookup, a type T is considered to have the following base types:
...
• If T is an interface-type, the base types of T are the base interfaces of T and the class type object.
Basically it's a fudge, to let the compiler understand that the members of object
will always really be available at execution time, even though they're not really members of the type of the expression involved for interfaces.
Any type that would implement the interface ultimately derives from object, hence Equals is always defined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With