Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
C#

C#: Use Interfaces for Contracts

- 15.07.26 - ErcanOPAK

📋 Interfaces = Contracts

Interfaces define contracts. Interfaces enable polymorphism, abstraction, loose coupling.

📝 Interface Basics

// Interface definition
public interface IUserService
{
    Task GetUserAsync(int id);
    Task CreateUserAsync(User user);
    Task UpdateUserAsync(User user);
    Task DeleteUserAsync(int id);
}

// Implementation
public class UserService : IUserService
{
    public async Task GetUserAsync(int id)
    {
        // Implementation
    }
    
    public async Task CreateUserAsync(User user)
    {
        // Implementation
    }
    
    public async Task UpdateUserAsync(User user)
    {
        // Implementation
    }
    
    public async Task DeleteUserAsync(int id)
    {
        // Implementation
    }
}

// Interface with properties
public interface IUser
{
    int Id { get; }
    string Name { get; set; }
    string Email { get; set; }
}

// Interface inheritance
public interface IRepository
{
    Task GetByIdAsync(int id);
    Task> GetAllAsync();
    Task AddAsync(T entity);
    Task UpdateAsync(T entity);
    Task DeleteAsync(int id);
}

public interface IUserRepository : IRepository
{
    Task GetByEmailAsync(string email);
}

🎯 Using Interfaces

// Dependency Injection
public class UserController
{
    private readonly IUserService _userService;
    
    public UserController(IUserService userService)
    {
        _userService = userService;
    }
}

// Multiple implementations
public class EmailNotification : INotification
{
    public async Task SendAsync(string message)
    {
        // Send email
    }
}

public class SmsNotification : INotification
{
    public async Task SendAsync(string message)
    {
        // Send SMS
    }
}

// Interface with default methods (C# 8+)
public interface ILogger
{
    void Log(string message);
    
    void LogError(string message)
    {
        Log($"ERROR: {message}");
    }
}

// Generic interface
public interface IValidator
{
    bool Validate(T entity);
}

// Interface segregation
public interface IReadable
{
    Task GetByIdAsync(int id);
    Task> GetAllAsync();
}

public interface IWritable
{
    Task AddAsync(T entity);
    Task UpdateAsync(T entity);
    Task DeleteAsync(int id);
}

// Interface Benefits
- Polymorphism
- Abstraction
- Loose coupling
- Testability
- Dependency injection

💡 Interface Tips

  • Use for abstraction
  • Enable polymorphism
  • Support dependency injection
  • Improve testability
  • Follow Interface Segregation Principle

“Interfaces define contracts. Polymorphism, abstraction, loose coupling. Essential for OOP.”

— Software Architect

Related posts:

.NET Core: Fix Memory Leaks by Understanding IDisposable and Using Patterns

.NET Core: Fix Memory Leaks by Understanding IDisposable and Using Patterns

C#: Use ValueTask for Async Methods That Often Return Synchronously

C#: Use ValueTask for Async Methods That Often Return Synchronously

.NET Core Connection Pooling — The Hidden Performance Giant

.NET Core Connection Pooling — The Hidden Performance Giant

Views: 10

Post navigation

C#: Use Properties for Encapsulation
Visual Studio: Use Live Share for Real-Time Collaboration

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (661)
  • Add Constraint to SQL Table to ensure email contains @ (619)
  • Average of all values in a column that are not zero in SQL (577)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (553)
  • Find numbers with more than two decimal places in SQL (481)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store
  • WordPress: Fix a Navigation Block Losing Submenu Items After Sync
  • Visual Studio: Fix CodeLens Reference Counts Stuck at Stale Numbers
  • C#: Fix a UTF-8 String Literal Behaving Differently in a Switch
  • Photoshop: Fix a Liquify Mesh Snapping Back on a Smart Object
  • The AI Prompt That Turns a Crash Dump Into a Short Repro Script

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,068)
  • Get the User Name and Domain Name from an Email Address in SQL (990)
  • How to add default value for Entity Framework migrations for DateTime and Bool (986)
  • Get the First and Last Word from a String or Sentence in SQL (865)
  • How to select distinct rows in a datatable in C# (859)

Recent Posts

  • Network Speed Monitor Windows Application v2.5.1
  • Ağ Hızı İzleyici Windows Uygulaması v2.5.1
  • How to make theater mode the default for Youtube
  • The AI Prompt That Audits a Function for Thread-Safety Gaps
  • Ajax: Fix a Fetch Request Caching a Response Despite No-Store

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com