简体   繁体   English

为什么C#编译器显式声明类型实现的所有接口?

[英]Why does the C# compiler explicitly declare all interfaces a type implements?

The C# compiler seems to explicitly note all interfaces it, and its base classes implement. C#编译器似乎明确地记下了它的所有接口,并且它的基类实现了。 The CLI specs say that this is not necesary. CLI规范说这不是必要的。 I've seen some other compilers not emit this explicitly, and it seems to work fine. 我已经看到其他一些编译器没有明确地发出它,它似乎工作正常。 Is there any difference or reason the C# does this? C#是否有这样的差异或原因?

The MSIL that the C# at the bottom generates for B is: 底部的C#为B生成的MSIL是:

.class private auto ansi beforefieldinit B
       extends A
       implements IAdvanced,
                  ISimple

It shouldn't need to specify ISimple, because A implements it as does IAdvanced. 它不需要指定ISimple,因为A和IAdvanced一样实现它。 C# code: C#代码:

interface ISimple {
    int Basic { get;  }
    int Zero { get;  }
}
interface IAdvanced : ISimple {
    string Major { get; }
}
class A : ISimple {
    int ISimple.Basic {
        get { return 1; }
    }
    int ISimple.Zero {
        get{ return 0;}
    }
}
class B : A, IAdvanced {
    string IAdvanced.Major {
        get { return "B"; }
    }
}

I don't think we can know any definitive answer here, unless we have the compiler developers drop in. However, we can guess about the reasons. 我不认为我们可以在这里知道任何确定的答案,除非我们让编译器开发人员介入。但是,我们可以猜测原因。 It could be: 它可能是:

  1. for optimization - perhaps it saves some work for the JIT compiler. 用于优化 - 也许它为JIT编译器节省了一些工作。
  2. for readability - makes it easier for human eyes to grasp what a type implements when looking at the MSIL output. 为了便于阅读 - 使人眼更容易掌握类型在查看MSIL输出时所实现的内容。
  3. because that's how the summer intern implemented it and since it works fine, nobody is going to change it just in case it breaks something. 因为这就是暑期实习生如何实施它并且因为它工作正常,所以没有人会改变它以防它破坏了什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么C#编译器不允许接口中的私有属性设置器? - Why C# compiler does not allows private property setters in interfaces? 接口无法声明类型问题C# - Interfaces Cannot Declare Type Issue C# 获取一个类继承自并在 C# 中实现的所有类型和接口 - Get all types and interfaces a class inherits from and implements in C# 为什么 C# 为其内置类型显式实现父接口? - Why does C# explicitly implement parent interfaces for its built-in types? 为什么Visual C#2008编译器认为显式声明和引用的类型是另一种未引用的类型? - Why would the Visual C# 2008 Compiler think one explicitly declared and referenced type is another unreferenced one? 返回类实现C#的接口类型 - Return the types of interfaces that a class implements C# 为什么C#将整数类型实现为结构而不是基本类型? - Why C# implements integer type as a struct and not as a primitive type? 声明实现通用接口的 C# Class - Declare C# Class That Implements a Generic Interface C#接口:是否可以在接口本身内引用实现接口的类型? - C# Interfaces: Is it possible to refer to the type that implements the interface within the interface itself? C# - 如何使用引用实现一组接口的任何对象的类型? - C# - How can I have an type that references any object which implements a set of Interfaces?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM