C# Extend Interface

Summary: in this tutorial, you’ll learn how to define an interface that extends a single interface or multiple interfaces.

Extending an interface

C# allows an interface to extend one or more interfaces. The following example illustrates how to define an interface that extends another interface:

First, define the IUndoable interface that has one method Undo():

interface IUndoable
{
    void Undo();
}Code language: C# (cs)

Second, define the IRedoable interface that extends the IUndoable interface:

interface IRedoable : IUndoable
{
    void Redo();
}Code language: C# (cs)

In this example, the IRedoable interface inherits all the members of the IUndoable interface. In other words, a class that implements the IRedoable interface must also provide implementations for Redo() method in the IRedoable interface and the Undo() method of the IUndoable interface.

Third, define the Input class that implements the IRedoable interface:

class Input : IRedoable
{
    public void Redo()
    {
        Console.WriteLine("Redo");
    }

    public void Undo()
    {
        Console.WriteLine("Undo");
    }
}Code language: C# (cs)

Finally, create an instance of the Input class and call the Undo() and Redo() methods:

var input = new Input();
input.Undo();
input.Redo();Code language: C# (cs)

Output:

Undo
RedoCode language: C# (cs)

Extending multiple interfaces

To define an interface that extends two or more interfaces, you use the following syntax:

interface IMyInterface: IMyInterface1, IMyInterface2, IMyInterface3
{
}Code language: C# (cs)

In this case, the MyInterface inherits all members from all the interfaces, including IMyInterface1, IMyInterface2, and IMyInterface3.

In other words, a class that implements the MyInterface needs provide implementations for all the members of those interfaces.

The following example illustrates how to define an interface that extends multiple interfaces.

First, define two interfaces IStore and IRetrieve:

interface IStore
{
    void Store(string data);
}

interface IRetrieve
{
    string? Retrieve();
}Code language: C# (cs)

Second, define an interface IMessage that extends the IStore and IRetrieve interfaces:

interface IMessage : IStore, IRetrieve
{
    void Delete();
}Code language: C# (cs)

The IMessage interface has its own method Delete(). It also inherits methods from the IStore and IRetrieve interfaces. It means that a class that implements the IMessage interface needs to implement all three methods.

Third, define the Message class that implements the IMessage interface:

class Message : IMessage
{
    private string? data = null;

    public void Delete()
    {
        data = null;
    }

    public string? Retrieve()
    {
        return data;
    }

    public void Store(string message)
    {
        data = message;
    }
}Code language: C# (cs)

Finally, create an instance of the Message class and call its methods:

// Program.cs

Message message = new();

message.Store("Hello");
Console.WriteLine(message.Retrieve());
message.Delete();Code language: C# (cs)

Output:

HelloCode language: C# (cs)

Summary

  • An interface can extend one or more interfaces.
  • A class that implements an interface needs to provide implementations for all the members of the interface and the members that the interface inherits from other interfaces.
Was this tutorial helpful ?