c# – 接口中的等于方法

据我所知,除了.net中的接口外,所有内容都来自于对象.但是我注意到当我按下“.”在界面名称出现Equals方法之后.当我按F12 for equals方法时,它会指向对象类中的equals方法.如果接口不是从对象类派生的,那么equals方法来自哪里?

解决方法

从C#4规范的第13.2节:

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).

第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. […]

第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.

基本上它是一种软糖,让编译器理解对象的成员在执行时总是真的可用,即使它们实际上不是接口所涉及的表达式类型的成员.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...