ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

C# Tutorial• 7 min read

Interfaces, Inheritance & Polymorphism

interface contracts, abstract classes, virtual / override methods, and dependency injection

C# supports single-class inheritance and multiple-interface implementation, enabling loose coupling and enterprise Dependency Injection.

Virtual Methods and Interface Contracts

Interfaces begin with an I prefix by convention (e.g. INotificationService).

Method overriding requires explicit virtual in the base class and override in the derived class.

is and as operators test and safely cast object types.

Dependency Injection Ready Interface
public interface IEmailService {
    Task SendEmailAsync(string recipient, string subject, string body);
}

public class SmtpEmailService : IEmailService {
    public async Task SendEmailAsync(string recipient, string subject, string body) {
        Console.WriteLine($"Dispatching email to {recipient} via SMTP...");
        await Task.Delay(100); // Simulate network latency
    }
}
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which keyword must be placed on a base class method in C# to allow derived classes to override it?