gpt4 book ai didi

c# - 接口(interface)中的等于方法

转载 作者:太空狗 更新时间:2023-10-29 20:53:19 26 4
gpt4 key购买 nike

据我所知,除了 .net 中的接口(interface)外,一切都派生自对象。但是我注意到当我按下“。”时。在接口(interface)名称 Equals 方法出现之后。当我为 equals 方法按 F12 时,它指向对象类中的 equals 方法。如果接口(interface)不是从对象类派生的,那么 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.

基本上,这是一种捏造,让编译器理解 object 的成员将在执行时始终真正可用,即使它们不是真正的成员接口(interface)所涉及的表达式类型。

关于c# - 接口(interface)中的等于方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8158215/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com