Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equals Method in Interfaces

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?

like image 460
Umut Derbentoğlu Avatar asked Nov 16 '11 20:11

Umut Derbentoğlu


People also ask

What is an equals method?

Definition and Usage. The equals() method compares two strings, and returns true if the strings are equal, and false if not.

Can we override equals method in interface?

We can override the equals method in our class to check whether two objects have same data or not.

What is equals () and hash code () method?

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.

Can you use equals () with objects?

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 .


2 Answers

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.

like image 187
Jon Skeet Avatar answered Nov 15 '22 06:11

Jon Skeet


Any type that would implement the interface ultimately derives from object, hence Equals is always defined.

like image 39
Motti Shaked Avatar answered Nov 15 '22 07:11

Motti Shaked