C# Interface

HTML
CSS
Bootstrap
JavaScript
C#
SQL
Salesforce Admin
Exercise
Study Material

Interfaces in C#

An interface in C# is a reference type that defines a contract for other classes to implement. It declares a set of methods, properties, events, or indexers that implementing classes must provide. However, it doesn’t contain any implementation details itself; it only defines the signatures of the members.

It is used to achieve multiple inheritance which can’t be achieved by class. It is used to achieve full abstraction because it cannot have a method body.

Basic Structure

public interface IExampleInterface

{

    // Method declarations

    void Method1();

    // Property declarations

    int Property1 { get; set; }

    // Event declarations

    event EventHandler Event1;

    // Indexer declaration

    string this[int index] { get; set; }

}

Example:

using System;

// Define an interface

public interface IShape

{

    double CalculateArea();

}

// Implement the interface in a class

public class Circle : IShape

{

    public double Radius { get; set; }

    public Circle(double radius)

    {

        Radius = radius;

    }

    public double CalculateArea()

    {

        return Math.PI * Radius * Radius;

    }

}

public class Rectangle : IShape

{

    public double Width { get; set; }

    public double Height { get; set; }

    public Rectangle(double width, double height)

    {

        Width = width;

        Height = height;

    }

    public double CalculateArea()

    {

        return Width * Height;

    }

}

class Program

{

    static void Main(string[] args)

    {

        // Creating instances of Circle and Rectangle

        Circle circle = new Circle(5);

        Rectangle rectangle = new Rectangle(4, 6);

        // Polymorphic behavior using the interface

        Console.WriteLine(“Circle area: ” + circle.CalculateArea());

        Console.WriteLine(“Rectangle area: ” + rectangle.CalculateArea());

    }

}

Output: Circle area: 78.5398163397448
Rectangle area: 24

Practices Tasks

1. Define an interface IVehicle with Start() and Stop() methods to represent vehicle functionalities. Then, create a class Car and inherit with IVehicle interface by providing implementations for Start() and Stop() methods. In your application’s entry point (e.g., Main method), instantiate a Car object and assign it to an IVehicle reference (IVehicle vehicle = new Car();) to leverage polymorphism. Finally, use the vehicle reference to call Start() and Stop() methods, which will output “Car started.” and “Car stopped.”

OUTPUT 

2. Define an interface IAnimal with MakeSound() and Eat() methods to represent animal behaviors. Then, create a class Dog that implements the IAnimal interface by providing implementations for MakeSound() and Eat() methods specific to a dog. In your application’s entry point (e.g., Main method), instantiate a Dog object and assign it to an IAnimal reference (IAnimal myPet = new Dog();) to leverage polymorphism. Finally, use the myPet reference to call MakeSound() and Eat() methods.

OUTPUT