HOME RESOURCES PLACEMENTS CONTACT FEEDBACK SUBSCRIBE
QUICK LINKS .NET Articles
.NET Training Tutorial
HR Interview Questions
.NET Interview Questions
SQL Interview Questions
JAVA Interview Questions

Interfaces in C# : Part 1
An interface looks very much like a class. The difference between an interface and a class is that, interface will have only declarartions for methods, properties, events and indexers and no implementations. Interfaces are inherited by classes and structs which must provide implementations for all the interface members declared.

Let us understand the purpose and usage of interfaces with an example. In the example below
  1. In the example below IExampleInterface is an interface with only one method InterfaceMethod();. This method has only the declaration and no implementation. It is compile time error to have a body for the method inside an interface. Interface should contain only the declarations and not implemenatations.
  2. Interface members are public by default. Access modifiers are not allowed on interface members. It is compile time error to declare a method inside an interface as shown below.
        public void InterfaceMethod();
  3. The MainClass inherits from IExampleInterface. As the MainClass is inheriting from IExampleInterface, there is a contract between the MainClass and IExampleInterface. The contract is - MainClass should implement the InterfaceMethod() inherited from IExampleInterface. So, the general rule is that, if a class or struct inherits from an interface, that particular class or struct should provide the implementation for all the interface members.
  4. A general naming convention is that interface names should be preceded by Capital I. In our example the name of the interface is IExampleInterface.

using System;

interface IExampleInterface

{

    void InterfaceMethod();

}

class MainClass : IExampleInterface

{

    public void InterfaceMethod()

    {

        Console.WriteLine("InterfaceMethod Implemented");

    }

    public static void Main()

    {

        MainClass MC = new MainClass();

        MC.InterfaceMethod();

    }

}
Multiple Interface Inheritance
A class can inherit from multiple interfaces at the same time, but a class cannot inherit from multiple classes at the same time. For a clear understaing of classes please read classes in C# article and for inheritance please read inheritance in C# article.

In our example below, the MainClass is inheriting from IInterface1 and IInterface2. So MainClass should implement both Interface1Method and Interface2Method.

using System;

interface IInterface1

{

    void Interface1Method();

}

interface IInterface2

{

    void Interface2Method();

}

class MainClass : IInterface1,IInterface2

{

    public void Interface1Method()

    {

        Console.WriteLine("Interface1Method Implemented");

    }

    public void Interface2Method()

    {

        Console.WriteLine("Interface2Method Implemented");

    }

    public static void Main()

    {

        MainClass MC = new MainClass();

        MC.Interface1Method();

        MC.Interface2Method();

    }

}
The above example can be rewritten as shown below. In the below example, IInterface2 is inheriting from IInterface1 and the MainClass is inherting from IInterface2. Hence the MainClass has to provide the implementation for both Interface1Method and Interface2Method.

using System;

interface IInterface1

{

    void Interface1Method();

}

interface IInterface2 : IInterface1

{

    void Interface2Method();

}

class MainClass : IInterface2

{

    public void Interface1Method()

    {

        Console.WriteLine("Interface1Method Implemented");

    }

    public void Interface2Method()

    {

        Console.WriteLine("Interface2Method Implemented");

    }

    public static void Main()

    {

        MainClass MC = new MainClass();

        MC.Interface1Method();

        MC.Interface2Method();

    }

}

Further Reading
Other related articles

Njoy Programming
   ByPrasad Cherukuri