C# 8 default interface methods

栏目: IT技术 · 发布时间: 4年前

内容简介:In the previous articles, we discussed C# 8async streams andpattern matching. In this article, we’ll look at C# 8 default interface methods.Before C# 8, it was not possible to add members to an interface without breaking the classes that implement the inte
C# 8 default interface methods

In the previous articles, we discussed C# 8async streams andpattern matching. In this article, we’ll look at C# 8 default interface methods.

Extending interfaces

Before C# 8, it was not possible to add members to an interface without breaking the classes that implement the interface. Because interface members were abstract, classes needed to provide an implementation. C# 8 allows us to extend an interface and provide a default implementation. The runtime (which also needs to support this feature) uses the default implementation when the class does not provide it:

interface IOutput
{
    void PrintMessage(string message);
    void PrintException(Exception exception)
        => PrintMessage($"Exception: {exception}");
}
class ConsoleOutput : IOutput
{
    public void PrintMessage(string message)
        => Console.WriteLine(message);
}

In this example, ConsoleOutput does not provide an implementation for PrintException . When PrintException is called against a ConsoleOutput instance, the default method from the IOutput interface will be called. ConsoleOutput might provide its own implementation.

A derived interface can provide a more appropriate default implementation by explicitly implementing the base member:

interface IA
{
    void M() { WriteLine("IA.M"); }
}
interface IB : IA
{
    void IA.M() { WriteLine("IB.M"); }
}

We explicitly implement the base member by including IA. in the name. Without IA. , the compiler would warn us to either make it explicit, or use the new keyword if we want to hide it.

C# 8 allows us to add static members in the interface that can be used by the default interface members:

interface IOutput
{
    private static string s_exceptionPrefix = "Exception";

    public static string ExceptionPrefix
    {
        get => s_exceptionPrefix;
        set => s_exceptionPrefix = value;
    }

    void PrintMessage(string message);

    sealed void PrintException(Exception exception)
        => PrintMessage($"{s_exceptionPrefix}: {exception}");
}

This example shows a private static field and a public static method to implement the ExceptionPrefix property that’s used by the sealed PrintException method. By adding the sealed keyword, this method can no longer be overridden.

C# 8 default interface methods

Everything you need to grow your career.

With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.

SIGN UP
C# 8 default interface methods

Code inheritance

C# supports inheriting from a single-base class and implementing multiple interfaces. Until C# 8, only the base class could provide code that is usable by the derived class. With C# 8, interfaces can provide code to their implementing classes.

In addition to that enhancement, we can use access modifiers on members and provide static members:

interface IOutput
{
    sealed void PrintException(Exception exception)
        => PrintMessageCore($"Exception: {exception}");

    protected void PrintMessageCore(string message);

    protected static void PrintToConsole(string message)
        => Console.WriteLine(message);
}

class ConsoleOutput : IOutput
{
    void IOutput.PrintMessageCore(string message)
    {
        IOutput.PrintToConsole(message);
    }
}

In this example, we see that the IOutput interface delegates the PrintMessageCore implementation to the derived class and provides a sealed implementation of PrintException that makes use of PrintMessageCore . The example also shows how the static protected PrintToConsole method can be called from a derived type.

This setup allows us to include the code that is common with the interface, allowing different classes to share this code without a common base class and thus enabling multiple inheritance and trait patterns.

C# 8 allows sharing code, but interfaces are still not allowed to have instance fields (state). Interface methods that require state either need to be abstract (so they are implemented in the class), or they need to accept the state as an argument (provided by the caller, like the implementing class). This approach avoids the inheritance diamond problem for state. For code, the diamond problem is solved by the compiler requiring additional members in ambiguous cases (which can then call the appropriate base).

Conclusion

In this article, we looked at C# 8 default interface methods. Default interface methods provide a way to extend interfaces with new members without breaking previous implementers. The new features also allow code to be shared between types, which enables multiple inheritance and trait patterns.  In the next article, we will look at nullable reference types.

C# 8 can be used with the .NET Core 3.1 SDK, which is available on Red Hat Enterprise Linux , Fedora , Windows, macOS, and other Linux distributions .


以上所述就是小编给大家介绍的《C# 8 default interface methods》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

引力

引力

保罗·X.麦卡锡 / 王正林 / 中信出版社 / 2018-3-1 / CNY 59.00

作者在书中提出一个全新的概念“网络引力”。我们全都受网络引力的影响,这种强大的力量正推动着数字经济前行。网络引力催生了像行星那样巨大的公司,它们以闪电般的速度击垮竞争对手,并且正在改变着商业、工作和娱乐休闲的面貌。作者在向读者展示这种令人震惊的现象的同时,还介绍了一系列独特的规则及其巨大力量,我们可以充分利用它们来创造属于自己的成功。这是在日益数字化的世界中为你赢得美好未来的一部理性指南,是一部互......一起来看看 《引力》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具