Home .NET New interface features in C# 8

New interface features in C# 8

by admin

New interface features in C# 8
First, let’s look at Andrew Troelsen and Philip Jepix’s definition of an interface: "An interface is just a named set of abstract members.Abstract methods are pure protocol because they do not provide their own standard implementations. The specific members defined by an interface depend on exactly what behavior it models. In other words, an interface expresses the behavior that a given class or structure may choose to support." And further on in the text: "Remember that when you define interface members, you don’t define an implementation scope for them. Interfaces are pure protocols and therefore an implementation is never provided for them."
Great, everything seems to be clear, approximately the same interpretation of the interface will be described in other books on the language, but in C# 8 interface is somewhat revised and added seemingly contradictory type features. Let’s look at the interfaces in C# 8 in more detail.

Default implementationsor default member implementations

By definition, if we inherit from an interface, we must implement the behavior of each interface member. In C# 8 it is now possible to implement a default interface member :

interface ICommand{void exec();// default implementationspublic void sendNotification(string mes){Console.WriteLine(mes);}}

The "exec" method is the usual abstract method in the interface that we’re all very used to, but the "sendNotification" method for the interface looks weird and offers its own default implementation.
Let’s see how it works :

public class AppFirstCommand : ICommand{public void exec(){// some actionConsole.WriteLine("Action complete!");}}

When implementing the interface, we are required to implement the abstract "exec" method and can omit the implementation of the "sendNotification" method. Convenient?

class Program{static void Main(string[] args){ICommand appFirstCommand = new AppFirstCommand();appFirstCommand.exec();appFirstCommand.sendNotification("Default implementations");}}

Execution Result :
Action complete!
Default implementations

To refer to a method of an interface with a default implementation, we need to declare a variable of reference interface type, i.e. a reference to the interface :

ICommand appFirstCommand = new AppFirstCommand();

Otherwise we can’t access the "sendNotification" method of the interface, because there is no implementation of this method in the "AppFirstCommand" class. It won’t work that way :

AppFirstCommand appFirstCommand = new AppFirstCommand();

And yes, we can also override the behavior of the "sendNotification" method:

public class AppSecondCommand : ICommand{public void exec(){// some actionConsole.WriteLine("Action complete!");}// override sendNotificationpublic void sendNotification(){Console.WriteLine("Override method from AppSecondCommand");}}

class Program{static void Main(string[] args){AppSecondCommand appSecondCommand = new AppSecondCommand();appSecondCommand.exec();appSecondCommand.sendNotification();}}

Execution Result :
Action complete!
Override method from AppSecondCommand

In this case we are directly accessing an instance of the "AppSecondCommand" class, since we have explicitly implemented the "sendNotification" method in that class.

How is the "Default implementations" approach convenient?

Default interface members allow you to add new members to an interface in future versions without breaking source code compatibility with existing implementations of that interface. Convenient.
Note that all C# access modifiers are available for default interface members

Interfaces can contain static members

interface IStaticMembers{private static string commandName = "Default command name";private void build(string name){commandName = name;}public void setCommandName(string name){build(name);}}

Interfaces can contain virtual members

But a class that inherits an interface cannot override a virtual member.
Only interfaces can override interface virtual members.

interface IVirtualMembers{public virtual void sendNotification(){Console.WriteLine("IVirtualMembers's sendNotification");}}interface IOverrideVirtualMembers : IVirtualMembers{void IVirtualMembers.sendNotification(){Console.WriteLine("IOverrideVirtualMembers's sendNotification");}}

It’s also worth noting that you can also define abstract members of interfaces in the same way, but it doesn’t make any sense.
So, a short list of new interface features in C# 8:

  • Default implementations or default member implementations;
  • Interfaces can contain static members;
  • Interfaces can contain virtual members.

All code from the article is on github

You may also like