Table of contents
1.
Introduction
2.
What are Interfaces?
2.1.
Uses
2.2.
How are they created?
2.3.
Examples
3.
Multiple Interfaces
4.
FAQs
5.
Key Takeaways
Last Updated: Mar 27, 2024

Interfaces in C#

Introduction

In this blog, we will learn about the Interfaces in C#,  so let's begin with an introduction of the Interface and then we will learn how we create them with their examples. Interfaces, like a class, can include members such as methods, properties, events, and indexers. However, interfaces will just include the member declarations. The implementation of the interface's members will be provided by a class that either implicitly or explicitly implements the interface.

What are Interfaces?

In C#, an interface is a class's blueprint. It's similar to an abstract class in that all of the methods specified within the interface are abstract methods. It is not capable of having a method body or being instantiated.

It is used to accomplish multiple inheritances that aren't possible with classes. Because it can't contain a method body, it's utilized to achieve complete abstraction.

It must be implemented using a class or struct. All of the methods defined inside the interface must be implemented by the class or struct that implements the interface.

Some considerations to keep in mind while utilizing a C# interface:

  • Interfaces, like abstract classes, cannot be used to construct objects.
  • Interfaces define what a class must do, not how it must do it.
  • Private members are not allowed in interfaces.
  • You must override all of an interface's methods when implementing it.
  • Properties and methods are allowed in interfaces, but fields and variables are not.
  • By default, interface members are abstract and public.
  • The interface will always be defined with the help of the keyword 'interface.'
  • An interface cannot contain a constructor (as it cannot be used to create objects)
  • Multiple inheritances are allowed via Interfaces, but not with classes.

Uses

Following are the advantages of using an Interface in C#:

  • It's a technique used for achieving loose coupling.
  • It's a technique used for achieving complete abstraction.
  • Used to make component-based programming a reality
  • Multiple inheritance and abstraction can be achieved.
  • Interfaces also provide apps with a plug-and-play design.

How are they created?

To use an interface, we have to use the keyword "interface." Its purpose is to give complete abstraction. This means that all interface members are defined with an empty body and are public and abstract by default. All of the methods defined in the interface must be implemented by a class that implements the interface.

Syntax for Declaration:

interface  <interface_name >

{

    // declare Events

    // declare indexers

    // declare methods 

    // declare properties

}

Syntax for Implementing:

class class_name : interface_name

Examples

Let's see the example of the interface in C#, which has the drives() method. Its implementation is provided by two classes: Car and Bike.

using System;  
public interface Drivable
{  
    void drives();  
}  
public class Car : Drivable 
{  
    public void drives()  
    {  
        Console.WriteLine("driving car...");  
    }  
}  
public class Bike : Drivable
{  
    public void drives()  
    {  
        Console.WriteLine("driving bike...");  
    }  
}  
public class TestInterface  
{  
    public static void Main()  
    {  
        Drivable d;  
        d = new Car();  
        d.drives();  
        d = new Bike();  
        d.drives();  
    }  
}  

Output:

driving car...

driving bike...

Multiple Interfaces

To use multiple interfaces, use a comma to separate them:

Example:

using System;  
interface One
{
  // interface method
  void someMethod(); 
}

interface Two 
{
   // interface method
   void someOtherMethod();
}

// Implementing multiple interfaces
class TempClass : One,Two 
{
  public void someMethod() 
  {
    Console.WriteLine("First interface's Method..");
  }
  public void someOtherMethod() 
  {
    Console.WriteLine("Second interface's Method...");
  }
}

class Program 
{
  static void Main(string[] args)
  {
    TempClass obj = new TempClass();
    obj.someMethod();
    obj.someOtherMethod();
  }
}

Output:

First interface's method..

Second interface's method...

We are done with the blog, let’s move to FAQS.

Also see,  Ienumerable vs Iqueryable

FAQs

  1. What is an interface in C#?
    In C#, an interface is a class's blueprint because all of the methods specified inside the interface are abstract methods, it's similar to an abstract class.
  2. Why do we need interfaces in C#?
    We require an interface to achieve security. Certain features are hidden, and only the most necessary details of an item are displayed (interface).
  3. Which is a better interface or abstract class in C#?
    When numerous classes must implement the interface, an interface is preferable to an abstract class. The interface's members cannot be static. Static is the sole full member of an abstract class. Multiple inheritances are not supported in C#; instead, interfaces are utilized to achieve multiple inheritances.
  4. When do we use Interfaces in C#?
    In C#, we utilize interfaces to capture commonalities between unrelated classes without connecting.
  5. What is the difference between interface and abstract class in C#?
    We may create multiple inheritances using the interface. As a result, we must begin the object design process with the interface, but abstract classes do not enable multiple inheritances; thus, they are always created after the interface.

Key Takeaways

In this article, we have extensively discussed C# Interface and learned about their uses and creation with their syntax and examples. You can visit here to learn more about C#.

Recommended Readings:

We hope that this blog has helped you enhance your knowledge regarding C# Interface and if you would like to learn more, check out our articles in the code studio library. Do upvote our blog to help other ninjas grow. Happy Coding!

Live masterclass