C# | Interface - GeeksforGeeks

22 Apr, 2020
Like a class, Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. The implementation of the interface’s members will be given by class who implements the interface implicitly or explicitly.
Interfaces specify what a class must do and not how.
Interfaces can’t have private members.
By default all the members of Interface are public and abstract.
The interface will always defined with the help of keyword ‘interface‘.
Interface cannot contain fields because they represent a particular implementation of data.
Multiple inheritance is possible with the help of Interfaces but not with classes.
Syntax for Interface Declaration:
interface  <interface_name >
{
    // declare Events
    // declare indexers
    // declare methods 
    // declare properties
}

Syntax for Implementing Interface:
class class_name : interface_name

To declare an interface, use interface keyword. It is used to provide total abstraction. That means all the members in the interface are declared with the empty body and are public and abstract by default. A class that implements interface must implement all the methods declared in the interface.
Example 1:// C# program to demonstrate working of // interfaceusing System;  // A simple interfaceinterface inter1{    // method having only declaration     // not definition    void display();}  // A class that implements interface.class testClass : inter1{          // providing the body part of function    public void display()    {        Console.WriteLine("Sudo Placement GeeksforGeeks");    }      // Main Method    public static void Main (String []args)    {                  // Creating object        testClass t = new testClass();                  // calling method        t.display();    }}Output:Sudo Placement GeeksforGeeks

// C# program to demonstrate working of // interfaceusing System;  // A simple interfaceinterface inter1{    // method having only declaration     // not definition    void display();}  // A class that implements interface.class testClass : inter1{          // providing the body part of function    public void display()    {        Console.WriteLine("Sudo Placement GeeksforGeeks");    }      // Main Method    public static void Main (String []args)    {                  // Creating object        testClass t = new testClass();                  // calling method        t.display();    }}
Sudo Placement GeeksforGeeks

Example 2:// C# program to illustrate the interfaceusing System;  // interface declarationinterface Vehicle {          // all are the abstract methods.    void changeGear(int a);    void speedUp(int a);    void applyBrakes(int a);}  // class implements interfaceclass Bicycle : Vehicle{          int speed;    int gear;          // to change gear    public void changeGear(int newGear)    {                  gear = newGear;    }          // to increase speed    public void speedUp(int increment)    {                  speed = speed + increment;    }          // to decrease speed    public void applyBrakes(int decrement)    {                  speed = speed - decrement;    }          public void printStates()     {        Console.WriteLine("speed: " + speed +                           " gear: " + gear);    }}  // class implements interfaceclass Bike : Vehicle {          int speed;    int gear;          // to change gear    public void changeGear(int newGear)    {                  gear = newGear;    }          // to increase speed    public void speedUp(int increment)    {                  speed = speed + increment;    }          // to decrease speed    public void applyBrakes(int decrement){                  speed = speed - decrement;    }          public void printStates()     {        Console.WriteLine("speed: " + speed +                           " gear: " + gear);    }      }  class GFG {          // Main Method    public static void Main(String []args)     {              // creating an instance of Bicycle         // doing some operations         Bicycle bicycle = new Bicycle();        bicycle.changeGear(2);        bicycle.speedUp(3);        bicycle.applyBrakes(1);                  Console.WriteLine("Bicycle present state :");        bicycle.printStates();                  // creating instance of bike.        Bike bike = new Bike();        bike.changeGear(1);        bike.speedUp(4);        bike.applyBrakes(3);                  Console.WriteLine("Bike present state :");        bike.printStates();    }}Output:Bicycle present state :
speed: 2 gear: 2
Bike present state :
speed: 1 gear: 1

// C# program to illustrate the interfaceusing System;  // interface declarationinterface Vehicle {          // all are the abstract methods.    void changeGear(int a);    void speedUp(int a);    void applyBrakes(int a);}  // class implements interfaceclass Bicycle : Vehicle{          int speed;    int gear;          // to change gear    public void changeGear(int newGear)    {                  gear = newGear;    }          // to increase speed    public void speedUp(int increment)    {                  speed = speed + increment;    }          // to decrease speed    public void applyBrakes(int decrement)    {                  speed = speed - decrement;    }          public void printStates()     {        Console.WriteLine("speed: " + speed +                           " gear: " + gear);    }}  // class implements interfaceclass Bike : Vehicle {          int speed;    int gear;          // to change gear    public void changeGear(int newGear)    {                  gear = newGear;    }          // to increase speed    public void speedUp(int increment)    {                  speed = speed + increment;    }          // to decrease speed    public void applyBrakes(int decrement){                  speed = speed - decrement;    }          public void printStates()     {        Console.WriteLine("speed: " + speed +                           " gear: " + gear);    }      }  class GFG {          // Main Method    public static void Main(String []args)     {              // creating an instance of Bicycle         // doing some operations         Bicycle bicycle = new Bicycle();        bicycle.changeGear(2);        bicycle.speedUp(3);        bicycle.applyBrakes(1);                  Console.WriteLine("Bicycle present state :");        bicycle.printStates();                  // creating instance of bike.        Bike bike = new Bike();        bike.changeGear(1);        bike.speedUp(4);        bike.applyBrakes(3);                  Console.WriteLine("Bike present state :");        bike.printStates();    }}
Bicycle present state :
speed: 2 gear: 2
Bike present state :
speed: 1 gear: 1

Advantage of Interface:
It is used to achieve loose coupling.
It is used to achieve total abstraction.
To achieve component-based programming
To achieve multiple inheritance and abstraction.
Interfaces add a plug and play like architecture into applications.My Personal Notes
arrow_drop_upSave
CSharp-Interfaces
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
C# | Delegates
Extension Method in C#
C# | String.IndexOf( ) Method | Set - 1
C# | Replace() Method
Introduction to .NET Framework
C# | Data Types
C# | Arrays
HashSet in C# with Examples
Common Language Runtime (CLR) in C#