What is the difference between an abstract class and an interface in C#?
Quick answer
An abstract class can hold state, constructors and partial implementation but a class can inherit only one, while an interface defines a contract that a class can implement many of.
Use an abstract class when related classes share fields, constructor logic or common behaviour, and you want to force subclasses to fill in the rest. A class can extend only one base class, so this is a strong coupling.
Use an interface to describe a capability (IDisposable, IComparable, IEmailSender) that unrelated types can implement, and to make code testable and replaceable through dependency injection. Since C# 8 interfaces may include default method implementations, but they still cannot hold instance fields. A common guideline: start with an interface for the contract, and add an abstract base class only if there is real shared code.
public interface INotifier { void Send(string message); }
public abstract class NotifierBase : INotifier
{
protected readonly ILogger Logger;
protected NotifierBase(ILogger logger) => Logger = logger;
public abstract void Send(string message);
}Key points
- Abstract class: shared state and code, single inheritance
- Interface: contract, multiple implementation
- Interfaces make dependency injection and testing easier