What is an Interface C#
What is an Interface C#? A Feature that enables developers to write efficient and scalable code.
Today, we will discuss What are Interfaces C#, their syntax, structure, and purpose. We will also compare C# Interface vs Abstract Class and explain how to implement interfaces in C#.
KEY TAKEAWAYS:
- Interface C# serves as a means to establish a collection of methods and additional members that can be implemented by various classes.
- C# Interfaces resemble abstract classes, yet they solely define method signatures and declarations for other members, without presenting any implementation specifics.
- By using C# interfaces, you can attain polymorphism in your code, enabling the utilization of objects from different classes that implement the same interface interchangeably.
- The implementation details are separated from the interface definition, enhancing the modularity and organization of your code.
- The interface in C# can be used to establish a contract that must be followed by other classes, thereby enhancing the reliability of your code.
C# What is Interface
An interface used to define a contract that a class must implement, but it does not provide any implementation itself. It can contain methods, events, properties, and indexers' declarations (but not implementation). An interface is a way to enforce a certain behavior in classes that implement it. So now we know what is an interface in c#, let's dig deep.
-
Syntax and Structure of Interfaces in C#
The syntax and structure of an interface in C# are as follows:
interface IExampleInterface { void Method1(); int Property1 { get; set; } event EventHandler Event1; }
The interface starts with the keyword "interface", followed by the interface name, and then a set of method signatures, property definitions, and event declarations.
-
Example of Interface in C#
Let's see an example of how to define an interface in C#:
interface IAnimal { void Move(); string Sound { get; } }
Here, we have defined an interface called "IAnimal" that has two members: a method called "Move()" and a read-only property called "Sound".
-
Key Characteristics of Interfaces in C#
The key characteristics of interfaces in C# are:
- An interface can only contain method, property, indexers, and event declarations. It cannot contain any implementation details.
- A class can implement multiple interfaces.
- An interface cannot be instantiated directly. It can only be implemented by a class.
- An interface can be used as a type, which enables you to write code that works with objects of different classes that implement the same interface.
Abstract Class vs Interface C#
C# abstract class cannot be instantiated directly and provide service as a base class for other classes, provides a common set of methods, properties, and events that its derived classes can implement.
-
Syntax of Abstract Class in C#
The syntax of an abstract class for method declaration is as follows:
public abstract class ExampleAbstractClass { public abstract void Method1(); public virtual void Method2() { Console.WriteLine("This is a virtual method."); } }
In this example, we define an abstract class called "ExampleAbstractClass" that has two members: an abstract method called "Method1()" and a virtual method called "Method2()".
-
Example of Abstract Class in C#
public abstract class Animal { public abstract void Move(); public virtual string Sound() { return "No sound"; } } public class Dog : Animal { public override void Move() { Console.WriteLine("The dog runs."); } public override string Sound() { return "Woof"; } }
In this example, we defined an abstract class called "Animal" that has an abstract method called "Move()" and a virtual method called "Sound()". We have also defined a derived class called "Dog" that implements "Move()" and overrides "Sound()".
An interface and an abstract class are both used to define methods that can be implemented by different classes. However, there are some key difference between abstract class and interface c#.
-
Benefits of Using Interface in C#
Interfaces are beneficial for the following reasons:
- They allow you to define a set of methods that can be implemented by different classes, without providing any implementation details.
- They provide a way to achieve polymorphism in C#, which means that you can use objects of different classes that implement the same interface interchangeably.
- They make your code more modular and easier to maintain because you can separate the implementation details from the interface definition.
- They allow you to define a contract that other classes must adhere to, which can provide help to ensure that your code is more reliable and easier to test.
-
Benefits of Using Abstract Class in C#
Abstract classes are beneficial for the following reasons:
- They allow you to provide a default implementation for some methods, which can be useful if you want to provide some common functionality to all derived classes.
- They can be used to define a base class for other classes, which can help to avoid code duplication and improve the structure of your code.
- They can be used to enforce a certain behavior in derived classes, by making certain methods abstract.
- They allow you to define a hierarchy of classes that share a common set of members, which can help to make your code more organized and easier to maintain.
Difference between Abstract Class and Interface in C#
Some of the difference between interface and abstract class in C# are listed here:
- An abstract class can contain implementation details, while an interface cannot.
- Concrete class can only inherit from one abstract class, but it can implement multiple interfaces.
- An abstract class can have constructors, but an interface cannot.
- Abstract class is used to define a base class for other classes, while an interface is used to define a set of members, like methods that must be implemented by different classes.
C# Interfaces vs Abstract Classes
Here's a simple code example of using an Interface vs Abstract Class C#:
public abstract class Shape
{
public abstract double Area();
}
public interface IColor
{
string Color { get; set; }
}
public class Rectangle : Shape, IColor
{
public double Width { get; set; }
public double Height { get; set; }
public string Color { get; set; }
public override double Area()
{
return Width * Height;
}
}
public class Circle : Shape, IColor
{
public double Radius { get; set; }
public string Color { get; set; }
public override double Area()
{
return Math.PI * Radius * Radius;
}
}
In this example, we have learned in C# what is an Interface and abstract class and what they have differences in them. We define an abstract class called "Shape" that has an abstract method called "Area()". We also define an interface called "IColor" that has a property called "Color". We then define two classes called "Rectangle" and "Circle" that inherit from "Shape" and implement "IColor".
We can also put the “Area()” method signature into Interface and implement that into the “Circle” and “Rectangle” classes. For learning more about Abstract Class vs Interface C#, please read our other articles available on this topic on this website.
Why use an Interface C#
You should use an interface in C# when you want to define a set of members like methods that can be implemented by different classes, without providing any implementation details. Interfaces are also useful when you want to achieve polymorphism in your code, or when you want to define a contract that other classes must adhere to.
Use of Interface in C#
-
Implementing Interface in C#
To implement an interface in C#, you must use the " : “operator in the class definition. Here's an example:
public interface IExampleInterface { void Method1(); void Method2(); } public class ExampleClass : IExampleInterface { public void Method1() { Console.WriteLine("Method1 implemented."); } public void Method2() { Console.WriteLine("Method2 implemented."); } }
In this example, we define an interface called "IExampleInterface" that has two methods, "Method1()" and "Method2()". We then define a class called "ExampleClass" that implements "IExampleInterface".
-
Using Interface with Multiple Classes in C#
You can use an interface with multiple classes in C# by implementing the interface in each class. We saw the same feature in one of the previous sections, here's another example:
public interface IAnimal { void Move(); } public class Dog : IAnimal { public void Move() { Console.WriteLine("The dog runs."); } } public class Cat : IAnimal { public void Move() { Console.WriteLine("The cat jumps."); } }
In this example, we define an interface called "IAnimal" that has a method called "Move()". We then define two classes called "Dog" and "Cat" that both implement "IAnimal".
Summary
In summary, we have learned what are Interfaces C#. The interface is a way to define a set of methods and some other members that can be implemented by different classes. It provides a way to achieve polymorphism and make your code more modular and organized. The interface is similar to an abstract class, but it only defines method signatures and declarations for some other members and does not provide any implementation details.
It was not possible to put everything into this single article, and we recommend you to learn more about Interfaces and Abstract class from other detailed articles we have for you on this website. And also consult Microsoft .NET and C# official documentation. C# Interfaces vs Abstract Classes, Difference between Interface and Abstract Class in C#, C# Abstract Class, Using Interfaces in C# !