The Wayback Machine - https://web.archive.org/web/20250918184338/https://www.geeksforgeeks.org/c-sharp/c-sharp-interface/
Open In App

Interface in C#

Last Updated : 16 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

An interface in C# is defined using the interface keyword. It serves as a blueprint that declares methods, properties, events or indexers without implementation. A class or struct that implements the interface must provide the actual implementation, either implicitly or explicitly.

There are some important points to remember, as mentioned below:

  • Interfaces cannot have private members.
  • By default, all the members of Interface are public and abstract.
  • The interface will always be defined with the help of the keyword interface.
  • Interfaces cannot contain fields because they represent an implementation of data.
  • We can achieve multiple inheritance with the interface but not with the classes.
  • We can implement multiple interfaces in a single class separated by commas.

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");
	}

	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 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

  • Loose coupling: It is used to achieve loose coupling rather than concrete implementations, we can decouple classes and reduce interdependencies.
  • Abstraction: Interface helps to achieve full abstraction.
  • Maintainability: It helps to achieve component-based programming that can make code more maintainable.
  • Multiple Inheritance: Interface used to achieve multiple inheritance and abstraction.
  • Define architecture: Interfaces add a plug and play like architecture into applications.
  • Modularity: Promote a cleaner and more modular design. By separating the definition of behavior from the implementation

Article Tags :

Explore