0 day streak
csharp / beginner
Snippet

Interface Contract Definition

Interfaces define a blueprint for classes. They allow for polymorphism and decouple the implementation from the usage, which is a key design pattern.

snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
11
public interface IEngine
{
void Start();
void Stop();
}
public class ElectricEngine : IEngine
{
public void Start() => Console.WriteLine("Silent start");
public void Stop() => Console.WriteLine("Power off");
}
Breakdown
1
public interface IEngine
Defines the mandatory methods that any engine must implement.
2
public class ElectricEngine : IEngine
Specifies that ElectricEngine fulfills the IEngine contract.