---
title: "How does C# implement the concept of interfaces and why are they used?"  
description: "How does C# implement the concept of interfaces and why are they used?"  
author: "Utpal Vishwas"  
published: 2023-07-02  
updated: 2023-07-04  
canonical: https://www.mindstick.com/forum/158927/how-does-c-sharp-implement-the-concept-of-interfaces-and-why-are-they-used  
category: "oops"  
tags: ["c#", "oops", "interface"]  
reading_time: 3 minutes  

---

# How does C# implement the concept of interfaces and why are they used?

How does C# implement the [concept](https://www.mindstick.com/blog/79/routing-concept-in-dot-net) of [interfaces](https://www.mindstick.com/articles/12100/interfaces-in-java-real-life-example) and why are they used?

## Replies

### Reply by Aryan Kumar

In C#, an interface is a contract that defines a set of methods that a class must implement. Interfaces are used to achieve a number of goals, including:

- **Abstraction:** Interfaces can be used to abstract away the implementation details of a class. This can make code more readable and maintainable.
- **Polymorphism:** Interfaces can be used to achieve polymorphism. Polymorphism allows you to treat objects of different types in the same way. For example, you can create a collection of objects that all implement the same interface, and then iterate through the collection using a generic loop.
- **Loose coupling:** Interfaces can be used to achieve loose coupling. Loose coupling means that two classes are not tightly coupled together. This can make code more flexible and reusable.

C# implements the concept of interfaces using the `interface` keyword. An interface definition looks like this:

C#

```plaintext
interface IAnimal
{
    void Speak();
    void Move();
}
```

The `IAnimal` interface defines two methods: `Speak()` and `Move()`. Any class that implements the `IAnimal` interface must provide implementations for both of these methods.

To implement an interface, a class uses the `implements` keyword. For example:

C#

```plaintext
class Dog : IAnimal
{
    public void Speak()
    {
        Console.WriteLine("Woof!");
    }

    public void Move()
    {
        Console.WriteLine("Run!");
    }
}
```

The `Dog` class implements the `IAnimal` interface by providing implementations for the `Speak()` and `Move()` methods.

Once a class has implemented an interface, it can be used in any context that requires an object that implements the interface. For example:

C#

```plaintext
List<IAnimal> animals = new List<IAnimal>();
animals.Add(new Dog());
animals.Add(new Cat());

foreach (IAnimal animal in animals)
{
    animal.Speak();
}
```

The `animals` list can contain objects of any type that implements the `IAnimal` interface. In this example, the list contains objects of type `Dog` and `Cat`. The `foreach` loop iterates through the list and calls the `Speak()` method on each object.

Interfaces are a powerful tool that can be used to achieve a number of goals in C#. They can be used to abstract away implementation details, achieve polymorphism, and achieve loose coupling.

### Reply by Gulshan Negi

Hello this is Gulshan Negi

Well, in C#, an interface is a language construct that defines a contract for classes to follow. It specifies a set of methods, properties, events, or indexers that a class implementing the interface must provide. The class that executes a connection point consents to stick to the point of interaction's agreement by carrying out every one of the individuals characterized in the connection point.

This is how defining interface in C

`public interface IShape`\
`{`\
`void Draw();`\
`double CalculateArea();`\
`}`\

Thanks


---

Original Source: https://www.mindstick.com/forum/158927/how-does-c-sharp-implement-the-concept-of-interfaces-and-why-are-they-used

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
