微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

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

原文地址:https://www.jb51.cc/csharp/98649.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐