加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 接口中的等于方法

发布时间:2020-12-15 18:29:15 所属栏目:百科 来源:网络整理
导读:据我所知,除了.net中的接口外,所有内容都来自于对象.但是我注意到当我按下“.”在界面名称出现Equals方法之后.当我按F12 for equals方法时,它会指向对象类中的equals方法.如果接口不是从对象类派生的,那么equals方法来自哪里? 解决方法 从C#4规范的第13.2节
据我所知,除了.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.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读