C# Interface (original) (raw)

Last Updated : 05 Apr, 2025

An interface is defined using the **interface keyword, similar to a class. An Interface is a blueprint that outlines a group of methods, properties, events, or indexers that a class or struct must implement. Unlike classes, it contains only the declaration of the members. The implementation of the interface’s members will be given by a class that implements the interface implicitly or explicitly. There are some important points to remember, as mentioned below:

**Example **1: Demonstrating the basic case for implementing the Interface.

C# `

// Demonstrate working of Interface using System;

interface inter1 { // method having only declaration // with no definition void display(); }

// Implementing inteface in Geeks class Geeks : inter1 { // providing the body part of function public void display() { Console.WriteLine("Demonstration of interface"); }

  // Main Method
public static void Main(String[] args)
{
    Geeks t = new Geeks();
    t.display();
}

}

`

Output

Demonstration of interface

**Example 2: Implementation of interfaces and the use of polymorphism in C#

C# `

// Using Interfaces and Polymorphism using System;

// interface declaration interface Vehicle { // abstract method. void speedUp(int a); }

// class implements interface class Bicycle : Vehicle { int speed;

// Method increase speed 
public void speedUp(int increment)
{
    speed = speed + increment;
}

// Method check speed
public void CheckSpeed()
{
    Console.WriteLine("speed: " + speed);
}

}

// class implements interface class Bike : Vehicle { int speed;

// to increase speed 
public void speedUp(int increment)
{
    speed = speed + increment;
}

public void CheckSpeed()
{
    Console.WriteLine("speed: " + speed);
}

}

class Geeks { public static void Main(String[] args) { // creating an instance of Bicycle // doing some operations Bicycle bicycle = new Bicycle(); bicycle.speedUp(3);

    Console.WriteLine("Bicycle present state :");
    bicycle.CheckSpeed();

    // creating instance of bike. 
    Bike bike = new Bike();
    bike.speedUp(4);

    Console.WriteLine("Bike present state :");
    bike.CheckSpeed();
}

}

`

Output

Bicycle present state : speed: 3 Bike present state : speed: 4

Advantages of Interfaces