简体   繁体   English

如何在子接口中提供默认实现?

[英]How do I provide a default implementation in a child interface?

If I have an interface IExampleInterface :如果我有一个接口IExampleInterface

interface IExampleInterface {
    int GetValue();
}

Is there a way to provide a default implementation for GetValue() in a child interface?有没有办法在子接口中为GetValue()提供默认实现? Ie: IE:

interface IExampleInterfaceChild : IExampleInterface {
    // Compiler warns that we're just name hiding here. 
    // Attempting to use 'override' keyword results in compiler error.
    int GetValue() => 123; 
}

After more experimentation, I found the following solution:经过更多的实验,我找到了以下解决方案:

interface IExampleInterfaceChild : IExampleInterface {
    int IExampleInterface.GetValue() => 123; 
}

Using the name of the interface whose method it is that you're providing an implementation for is the right answer (ie IParentInterface.ParentMethodName() =>... ).使用您为其提供实现的方法的接口的名称是正确的答案(即IParentInterface.ParentMethodName() =>... )。

I tested the runtime result using the following code:我使用以下代码测试了运行时结果:

class ExampleClass : IExampleInterfaceChild {
        
}

class Program {
    static void Main() {
        IExampleInterface e = new ExampleClass();

        Console.WriteLine(e.GetValue()); // Prints '123'
    }
}

In C# 8.0+ the interfaces can have a default method:在 C# 8.0+ 中,接口可以有一个默认方法:

https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#default-interface-methods https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#default-interface-methods

Otherwise if you are using lower version on C# due to using.Net Framework, you may use an abstract class.否则,如果由于使用 .Net Framework 而在 C# 上使用较低版本,则可以使用抽象 class。 but If you want your classes to be able to implement several interfaces, this option may not work for you:但是如果您希望您的类能够实现多个接口,则此选项可能不适合您:

public abstract class ExampleInterfaceChild : IExampleInterface {
    int GetValue() => 123; 
}

暂无
暂无

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

相关问题 我如何提供通用子财产 - How do I provide a generic child property 我如何提供ProjectItem.get_FileNames(i)的实现? - How do I provide an implementation of ProjectItem.get_FileNames(i)? (ab)使用CoClassAttribute为接口提供默认实现是否可以? - Is it ok to (ab)use CoClassAttribute to provide a default implementation for an interface? C# - 使用扩展方法提供默认接口实现 - C# - using extension methods to provide default interface implementation 接口实现不提供async怎么办 - What to do when interface implementation does not provide async 如果我总是必须在实现中再次设置它们,那么在接口中设置默认值有什么好处? - What do I gain by setting the default values in the interface if I always have to set them again in the implementation? C#中如何使用接口默认实现 - How to use interface default implementation in C# 当我在 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 C# allow me to declare a default interface implementation as “sealed”, but then behave inconsistently when I do? 接口的默认实现 - Default implementation of Interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM