KodeBLOG

Kode Blog - Inspiring And Empowering Developers.

Home About Us Courses Categories Blog Contact Us

C# Interface

Introduction

An interface defines a contract that all classes that derive from it must honour. An interface is used to describe methods that all derived classes must implement.

Syntax for declaring interfaces

The keyword interface is used to define interfaces. It is recommended to prefix interfaces with a capital leter I to n. You can use any valid name to name an interface but is is considered a best practice if you use capital I

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpInterface
{
    interface IConnectToDB
    {

    }
}

HERE,

  • the keyword interface is used define an interface IConnectToDB

Rules for Interfaces

  • an interface cannot be sealed.
  • an interface cannot be private or static
  • interface methods cannot contain any implementation code
  • by default, all methods are implicitly public. Explicitly declaring a method as public in an interface results into a compile-time error
  • a class can inherit from more than one interface.

C# Interface example

The following code defines an interface for connecting to the database.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpInterface
{
    interface IConnectToDB
    {
        bool connect();

        bool disconnect();

        bool executeQuery();

        bool executeSelectQuery();
    }
}

We will now create concrete classes that implement the interface.

Add a new class SQLServer.cs

Add the following code to SQLServer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpInterface
{
    class SQLServer : IConnectToDB
    {
        public bool connect()
        {
            return true;
        }

        public bool executeSelectQuery()
        {
            return true;
        }

        public bool disconnect()
        {
            return true;
        }

        public bool executeQuery()
        {
            return true;
        }
    }
}

HERE,

  • class SQLServer : IConnectToDB defines a class SQLServer that inherits from the interface IConnectToDB
  • public bool connect(){...} implements the connect method
  • public bool disconnect(){...} implements the disconnect method
  • public bool executeQuery(){...} implements the executeQuery method
  • public bool executeSelectQuery(){...} implements the executeSelectQuery method

Create another new class MySQL.cs

Add the following code to MySQL.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpInterface
{
    class MySQL : IConnectToDB
    {
        public bool connect()
        {
            return true;
        }

        public bool executeSelectQuery()
        {
            return true;
        }

        public bool disconnect()
        {
            return true;
        }
    }
}

Press F6 on the keyboard

You will get the following compile-time error

'CSharpInterface.MySQL' does not implement interface member 'CSharpInterface.IConnectToDB.executeQuery()'

As you can see from the above results, the MySQL.cs class violates the interface contract by not implementing the executeQuery() method.

Add the following executeQuery() definition to MySQL.cs

public bool executeQuery()
{
    return true;
}

Press F6 on the keyboard

This time, the project will build without error compile-time errors. As you can see from this example, interfaces can be used to ensure that classes implement all of the methods specified in the interface contract.

Summary

An interface is a contract that specifies what needs to be implemented. All classes that derive from the interface must honour the contract by implementing all of the methods specified in the interface.

Tutorial History

Tutorial version 1: Date Published 2015-08-17