Interface Equality Method

As I know, everything comes from an object, except for interfaces in .net. But I noticed that when I click "." the Equals method appears after the interface name. And when I press F12 for the equals method, it directs the equals method to the object class. If the interfaces are not derived from the object class, where is the method coming from?

+6
source share
3 answers

From section 13.2 of the C # 4 specification:

Note that the members in a class object are not, strictly speaking, members of any interface (§13.2). However, members in a class object are accessible by searching for an element in any type of interface (§7.4).

And section 7.4:

  • First, a set of available members is defined with the name N:
    • ...
    • Otherwise, the set consists of all available (§3.5) members with the name N in T, including inherited members and available members with the name N in the object. [...]

And section 7.4.1:

For member search purposes, type T is considered to have the following basic types:

...

• If T is an interface type, the base types of T are the base interfaces of T and the object of the class type.

This is basically a fiction that allows the compiler to understand that object members will always be available at run time, even if they are not members of an expression type for interfaces.

+14
source

Any type that implements an interface ultimately comes from an object, so Equals is always defined.

+2
source

interfaces can only ever be on objects.

+1
source

Source: https://habr.com/ru/post/901655/


All Articles