C# Interface

Summary: in this tutorial, you’ll learn about the C# interface and how to use the interfaces in programming to make the application more extensible.

Introduction to the C# interface

An interface defines a contract between classes. To define an interface, you use the interface keyword followed by the interface name like this:

interface InterfaceName
{
}Code language: C# (cs)

By convention, interface names in C# start with the letter I in uppercase like IReadable and IWritable. Like a class, an interface can contain the following members:

But unlike a class, the members of an interface only have declarations. In other words, they do not contain implementations. For example:

interface IMyInterface
{
    void MyMethod();
}Code language: C# (cs)

All the members of an interface are public by default. Therefore, you don’t need to use the public keyword for the interface’s members.

Note that starting with C# 8 or later, interface members can have default implementations.

Like an abstract class, you cannot create a new instance of an interface. To use an interface, you need to implement it from your class. For example:

class MyClass: IMyInterface
{
    public void MyMethod()
    {
        // implementation
    }
}Code language: C# (cs)

In this example, we define the MyClass that implements the IMyInterface interface. The syntax used for implementing an interface is like the syntax used for inheriting from a class.

In this example, we define the MyClass that implements the IMyInterface interface. The access modifier of the MyMethod() is public.

The class that implements an interface need to provide all the implementation of the interface members.

A class can inherit from a single class but can implement multiple interfaces. For example:

class MyClass : IMyInterface1, IMyInterface2
{
   //...
}Code language: C# (cs)

C# interface example

The following example defines a simple interface called IReadable with one method Read():

interface IReadable
{
    string Read();
}Code language: C# (cs)

The following defines the ConsoleReader class that implements the IReadable interface:

class ConsoleReader : IReadable
{
    public string Read()
    {
        return Console.ReadLine() ?? string.Empty;
    }
}Code language: C# (cs)

Because the ConsoleReader class implements the IReadable interface, it must implement the Read() method. This is called design by contract.

The following creates a new instance of the ConsoleReader class and read a string from the console:

// Program.cs

IReadable reader = new ConsoleReader();
string input = reader.Read();Code language: C# (cs)

If you want to read the contents of a text file, you can define a new class FileReader that implements the IReadable interface:

class FileReader : IReadable
{
    public string Filename { get; set; }

    public FileReader(string filename)
    {
        Filename = filename;
    }

    public string Read()
    {
        return File.ReadAllText(Filename);
    }
}Code language: C# (cs)

The FileReader class has the Filename as property and implements the Read() method of the IReadable interface, which reads the contents of a text file specified by the Filename.

The following creates a new instance of the FileReader class and reads all contents of the file located at C:\temp\test.txt:

// Program.cs
IReadable reader = new FileReader(@"C:\temp\test.txt");
string content = reader.Read();

Console.WriteLine(content);Code language: C# (cs)

Both the ConsoleReader and FileReader classes implement the IReadable interface. It means that you can easily swap them. For example, the following defines a method that accepts an interface IReadable:

string UppercaseReader(IReadable reader)
{
    return reader.Read().ToUpper();
}Code language: C# (cs)

The UppercaseReader() function calls the Read() method and returns the string in uppercase.

When calling the UppercaseReader() function, you can pass an instance of any class that implements the IReadable interface. For example:

// Program.cs

string UppercaseReader(IReadable reader)
{
    return reader.Read().ToUpper();
}

IReadable[] readers = { 
    new ConsoleReader(),
    new FileReader(@"C:\temp\test.txt") 
};

foreach (var reader in readers)
{
    Console.WriteLine(UppercaseReader(reader));
}Code language: C# (cs)

In this example, we create an array that contains two instances of classes that implement the IReadable interface and pass them to the UppercaseReader() function.

Summary

  • An interface is a contract between classes.
  • By convention, an interface name starts with the letter I.
  • An interface contains members including methods, properties, indexers, and events without implementations.
  • A class that implements an interface must provide implementations for the members of the interface.
  • A class can implement multiple interfaces.
Was this tutorial helpful ?