Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

C# 8 default interface methods

March 3, 2020
Tom Deseyn
Related topics:
.NET
Related products:
Red Hat OpenShift

    In the previous articles, we discussed C# 8 async streams and pattern 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.

    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.

    Last updated: February 22, 2024

    Recent Posts

    • Testing infrastructure red teaming with abliterated models

    • Build an enterprise RAG system with OGX

    • Solutions for SELinux MCS challenges with GitLab runners

    • MCP servers vs. skills: Choosing the right context for your AI

    • How to route external and local LLMs with Models-as-a-Service

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.