C# Interface

In c#, interface is same like class but only difference is class can contain both declarations and implementation of methods, properties and events but interface will contain only the declarations of methodsproperties and events that a class or struct can implement.

 

An interface in c# is more like a contract and the class or struct that implements an interface must provide an implementation for all the members that are specified in the interface definition.

 

Generally, c# will not support multiple inheritance of classes but that can be achieved by using interface. In addition, a structure in c# cannot be inherited from another structure or class but that can be inherited by using interfaces.

 

In c#, we can define an interface by using interface keyword. Following is the example of defining an interface using interface keyword.

 

interface IUser

{

    void InsertDetails();

}

If you observe above code snippet, we defined an interface (IUser) using interface keyword with InsertDetails method signature. Now, the IUser interface can be implemented by any class or struct by providing a definition for InsertDetails method.

 

To implement an interface in a class or structure the syntax will be like class ClassName : Interface Name. Following is the example of implementing an interface in a class.

 

class User : IUser

{

    void InserDetails()

    {

        // Method Implementation

    }

}

If you observe code snippet, we inherited an interface (IUser) in a class (User) and implemented a defined interface method in class.

 

In c#, an interface cannot be instantiated directly, but it can be instantiated by a class or struct that implements an interface. Following is the example of creating an instance for the interface in c# programming language.

 

IUser u = new User();

In c#, a class can inherit only from one class but we can implement a multiple interfaces in a class or struct.

 

By default, the members of interface are public and we are not allowed to include any other access modifiers. In c#, an interface can contain a methodsproperties, events, indexers but it can’t contain constants, fields, operators, instance constructors, finalizers or types.

C# Interface Example

Following is the example of creating and implementing an instance using a class in c# programming language.

 

using System;

 

namespace Tutlane

{

    interface IUser

    {

        void GetDetails(string x);

    }

    class User : IUser

    {

        public void GetDetails(string a)

        {

            Console.WriteLine("Name: {0}", a);

        }

    }

    class User1 : IUser

    {

        public void GetDetails(string a)

        {

            Console.WriteLine("Location: {0}", a);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            IUser u = new User();

            u.GetDetails("Suresh Dasari");

            IUser u1 = new User1();

            u1.GetDetails("Hyderabad");

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe above example, we created an interface IUser and the two classes “User & User1” implemented an interface IUser by providing an implementation for GetDetails() method and we created an instance for interface “IUser” using User & User1 classes.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Interface Example Result

C# Multiple Inheritance with Interface

As discussed, c# will not support multiple inheritance of classes but that can be achieved by using interface.

 

Following is the example of implementing a multiple inheritance using interfaces in c# programming language.

 

using System;

 

namespace Tutlane

{

    interface IName

    {

        void GetName(string x);

    }

    interface ILocation

    {

        void GetLocation(string x);

    }

    interface IAge

    {

        void GetAge(int x);

    }

    class User : IName, ILocation, IAge

    {

        public void GetName(string a)

        {

            Console.WriteLine("Name: {0}", a);

        }

        public void GetLocation(string a)

        {

            Console.WriteLine("Location: {0}", a);

        }

        public void GetAge(int a)

        {

            Console.WriteLine("Age: {0}", a);

        }

    }

    class Program

    {

        static void Main(string[] args)

        {

            User u = new User();

            u.GetName("Suresh Dasari");

            u.GetLocation("Hyderabad");

            u.GetAge(32);

            Console.WriteLine("\nPress Enter Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe example, we created a multiple interfaces and implementing those interfaces using User class to achieve multiple inheritance.

 

When we execute above c# program, we will get the result like as shown below.

 

C# Multiple Inheritance with Interface Example Result

 

This is how we can achieve multiple inheritance using interface in c# programming language. 

C# Interface Overview

Following are the important properties of interface in c# programming language.

 

  • In c#, interface is like an abstract class and it can contain only declarations of members such as methodsproperties, indexers and events.
  • By default, the members of interface are public and we are not allowed to include any other access modifiers.
  • In c#, an interface cannot be instantiated directly, but it can be instantiated by a class or struct that implements an interface.
  • The class or struct that implements an interface must provide an implementation for all the members that are specified in the interface definition.
  • The class or struct can implement multiple interfaces. 

Difference between Abstract Class and Interface

Following are the differences between abstract class and interface in c# programming lagnauge.

 

Abstract ClassInterface
In c#, the abstract classes cannot be instantiated In c#, an interface cannot be instantiated directly, but it can be instantiated by a class or struct that implements an interface.
An abstract class can contain both declarations and implementations of methodsproperties, etc. An interface can contain only declarations of methodsproperties, indexers and events.
The members of abstract class can contain different access modifiers. By default, all the members of interface are public and we are not allowed to include any other access modifiers.
class can inherit only one abstract class. A class can inherit multiple interfaces.
class that is derived from abstract class must implement all inherited abstract methods and accessors. The class or struct that implements an interface must provide an implementation for all the members that are specified in the interface definition.
 

PREVIOUS

C# Abstract
 

NEXT

C# Delegates