简体   繁体   English

为什么 C# 允许我将默认接口实现声明为“密封”,但这样做时却表现不一致?

[英]Why does C# allow me to declare a default interface implementation as “sealed”, but then behave inconsistently when I do?

New C# .NET Core Console App:新的 C# .NET 核心控制台应用程序:

using System;

namespace Interface_Sealed
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(new C().X);
            Console.WriteLine((new C() as I).X);
            Console.ReadKey();
        }

        interface I
        {
            sealed int X => 0;
        }

        class C: I
        {
            public int X => 1;
        }
    }
}

When run, this prints "10" to the console.运行时,这会将“10”打印到控制台。 The first ".X" is calling the implementation of X in the class C, and the second one is calling the default implementation on the interface I.第一个“.X”是调用class C中X的实现,第二个是调用接口I上的默认实现。

Why does C# allow me to declare the default implementation of X as "sealed", then allow me to override it with a different implementation without complaining?为什么 C# 允许我将 X 的默认实现声明为“密封”,然后允许我用不同的实现覆盖它而不抱怨? Why does it make a difference whether I access X through a reference of type I or of type C?为什么我通过类型 I 或类型 C 的引用访问 X 会有所不同?

NB.注意。 If you implement IX in C explicitly instead of implicitly, you get CS0539 "'Program.C.X' in explicit interface declaration is not found among members of the interface that can be implemented".如果您在 C 中显式地而不是隐式地实现 IX,您会得到 CS0539“'Program.C.X' 在显式接口声明中找不到在可以实现的接口的成员中”。 The Microsoft documentation page for this error states that it occurs when "an attempt [is] made to explicitly declare an interface member that does not exist". 此错误的 Microsoft 文档页面指出,当“尝试 [is] 显式声明不存在的接口成员”时会发生此错误。 Well, that's not true, is it?好吧,这不是真的,是吗? It does exist.它确实存在。

Why does C# allow me to declare the default implementation of X as "sealed", then allow me to override it with a different implementation without complaining?为什么 C# 允许我将 X 的默认实现声明为“密封”,然后允许我用不同的实现覆盖它而不抱怨?

It doesn't.它没有。 You haven't overridden the member, merely made a new member with the same name.您没有覆盖该成员,只是创建了一个具有相同名称的新成员。 The fact that the member that returns 1 isn't called when the type is the interface type demonstrates this.当类型是接口类型时,调用返回1的成员这一事实证明了这一点。 This is entirely consistent with sealed members in classes inheriting from another.这与从另一个继承的类中的密封成员完全一致。 You aren't preventing a class implementing the interface from having it's own member with that name and signature, you're merely preventing it being the one that's associated with the interface (or base class).您并没有阻止实现接口的 class 拥有具有该名称和签名的自己的成员,您只是阻止它成为与接口(或基类)关联的成员。

Why does it make a difference whether I access X through a reference of type I or of type C?为什么我通过类型 I 或类型 C 的引用访问 X 会有所不同?

Because both I and C have two different members that just coincidentally happen to have the same name and signature, but different behavior.因为IC都有两个不同的成员,碰巧有相同的名称和签名,但行为不同。 They would only have the same behavior if you were able to override the interface's member, thereby changing the behavior of the member when called via the interface.只有当您能够覆盖接口的成员时,它们才会具有相同的行为,从而在通过接口调用时更改成员的行为。

Well, that's not true, is it?好吧,这不是真的,是吗?

No, it is true.不,这是真的。 That member isn't a member that can be implemented because it's sealed.该成员不是可以实现的成员,因为它是密封的。 The member is there, but you can't change it's implementation, hence the error.该成员在那里,但您不能更改它的实现,因此会出现错误。

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

相关问题 当我在 class 派生形式的接口中调用它时,为什么需要将“this”强制转换为 C# 8.0 中具有默认实现的接口? - Why do I need to cast 'this' to an interface with a default implementation in C# 8.0 when I call it in the class derived form that interface? 为什么“接口没有实现”? - C# - Why does “interface have no implementation”? - C# 为什么在C#中将静态类声明为密封和抽象? - Why declare static classes as sealed and abstract in C#? C#8默认接口实现是否允许多重继承 - Do C# 8 default interface implementations allow for multiple inheritance 将密封类转换为可能实现的接口时,为什么会出现编译错误? - Why do I get a compilation error when casting a sealed class to an interface it might implement? 为什么C#编译器插入显式接口实现? - Why does the C# compiler insert an explicit interface implementation? 为什么需要在C#显式实现中将'this'转换为接口类型? - Why do I need to cast 'this' to interface type in a C# explicit implementation? C# 8 默认接口实现与继承 - C# 8 default interface implementation and inheritance C#调用接口方法默认实现 - C# call interface method with default implementation 为什么C#SerializedAttribute是密封的? - Why is the C# SerializedAttribute is sealed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM