csharp / beginner
Snippet
Flexible Implementation Interfaces
Interfaces define a contract that different classes can implement, allowing you to swap behaviors easily in your code.
snippet.cs
csharp
1
2
3
4
5
6
7
8
9
10
11
public interface IMessageService {void Send(string message);}public class EmailService : IMessageService {public void Send(string message) => System.Console.WriteLine($"Email: {message}");}public class SmsService : IMessageService {public void Send(string message) => System.Console.WriteLine($"SMS: {message}");}
Breakdown
1
public interface IMessageService
Defines the blueprint that all messaging services must follow.
2
void Send(string message);
The method signature that implementing classes must provide logic for.