C# Interface

In c#, the interface is same as a class, but the only difference is class can contain both declarations and implementation of methods, properties, and events, but the 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,. The class or struct that implements an interface must provide an implementation for all the members specified in the interface definition.

 

Generally, c# will not support multiple inheritance of classes, but that can achieve by using an interface. Also, a structure in c# cannot be inherited from another structure or class, but that can inherit 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 the above code snippet, we defined an interface (IUser) using interface keyword with the InsertDetails method signature. Now, the IUser interface can be implemented by any class or struct by providing a definition for the InsertDetails method.

 

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

 

class User: IUser
{
    void InserDetails()
    {
       // Method Implementation
    }
}

If you observe the code snippet, we inherited an interface (IUser) in a class (User) and implemented a defined interface method in a 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 the c# programming language.

 

IUser u = new User();

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

 

By default, the members of an interface are public, and we are not allowed to include any other access modifiers. In c#, an interface can contain 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 the 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 the 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 the above c# program, we will get the result 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 achieve by using the interface.

 

Following is the example of implementing a multiple inheritance using interfaces in the 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 multiple interfaces and implementing those interfaces using User class to achieve multiple inheritance.

 

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

 

C# Multiple Inheritance with Interface Example Result

 

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

C# Interface Overview

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

 

  • In c#, the 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 an 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 specified members in the interface definition.
  • The class or struct can implement multiple interfaces. 

Difference between Abstract Class and Interface

The following are the differences between abstract class and interface in c# programming language.

 

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 the abstract class can contain different access modifiers. By default, all the members of an 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 the 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 specified members in the interface definition.