Interfaces

manviii_274 total contributions
Published Sep 10, 2023
Contribute to Docs
An interface in C# is a contract that defines a set of methods, properties, events, and indexers that a class or struct must implement. Interfaces cannot be instantiated directly, but they can be implemented by classes and structs. They are one of several tools for implementing object-oriented design in C#.
Syntax
An interface in C# is created using the interface
keyword. The syntax for defining an interface in C# is:
interface MyInterface
{
void MyMethod();
string MyProperty { get; set; }
event EventHandler MyEvent;
}
To implement an interface in C#, you use the :
symbol, followed by the name of the interface. The syntax for implementing an interface is as follows:
class MyClass : MyInterface
{
public void MyMethod()
{
// implementation of MyMethod()
}
public string MyProperty
{
get { return "MyValue"; }
set { }
}
public event EventHandler MyEvent;
}
Example
The following example shows implementation of an interface:
using System;// Create an interface called IAnimalinterface IAnimal{// Define a method called Speak()void Speak();}// Create a class called Dog that implements the IAnimal interfaceclass Dog : IAnimal{public void Speak(){Console.WriteLine("Woof!");}}// Create a class called Cat that implements the IAnimal interfaceclass Cat : IAnimal{public void Speak(){Console.WriteLine("Meow!");}}// Create a main method to test the programpublic class Program{public static void Main(string[] args){// Create a Dog objectDog dog = new Dog();// Call the Speak() method on the Dog objectdog.Speak();// Create a Cat objectCat cat = new Cat();// Call the Speak() method on the Cat objectcat.Speak();}}
The output for the above code is:
Woof!Meow!
Use of Interfaces
Interfaces can be used in C# to achieve a number of different goals, including:
Abstraction
: Interfaces can be used to abstract away the implementation details of a class or struct. This can make code more modular and easier to understand.Multiple inheritance
: C# does not support multiple inheritance of classes, but it does support multiple inheritance of interfaces. This allows a class to inherit the functionality of multiple different interfaces.Plug-and-play
: Interfaces can be used to create a “plug-and-play” architecture. This means that different classes or structs can be easily swapped in and out, as long as they implement the same interfaces.
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.