Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Interface Polymorphism Explained with Code Examples

C# interface polymorphism lets different classes be called through a common interface type. See how runtime dispatch works, how it compares with abstract classes, and when it helps.

~6 min readC#
#C# Interfaces#Polymorphism#OOP Design#Dependency Injection
Diagram showing three different class shapes connected to a central interface node, illustrating C# interface polymorphism

C# interface polymorphism is the ability of different classes to be treated through a common interface type while each implementation supplies its own behavior for the members the interface declares. The compiler allows a variable typed as the interface to hold any object whose class implements that interface, and the runtime dispatches calls to the actual implementation rather than to a fixed method. This separation between what a caller depends on and which concrete class satisfies that contract is what makes interface polymorphism useful in real codebases.

What Interface Polymorphism Requires at the Type Level

For interface polymorphism to work, a class must declare that it implements the interface, and the class hierarchy must supply concrete bodies for the members declared by the interface. In classic C# (before C# 8), interfaces contain only signatures, so the implementing class or one of its base classes must provide each member's implementation. C# 8 and later also allow default implementations, but the core polymorphic contract remains the same: callers use the interface type, and the actual implementation is resolved at runtime. Consider a simple contract for a payment processor:

csharp
public interface IPaymentProcessor { bool ProcessPayment(decimal amount); }

Two classes can implement the same interface with completely different behavior:

csharp
public class CreditCardProcessor : IPaymentProcessor { public bool ProcessPayment(decimal amount) { // Charge the card through a gateway. return amount > 0; } } public class PayPalProcessor : IPaymentProcessor { public bool ProcessPayment(decimal amount) { // Redirect to PayPal and wait for confirmation. return amount > 0 && amount < 10000; } }

The caller never needs to know which concrete class it is dealing with. It only needs the interface reference:

csharp
IPaymentProcessor processor = GetProcessor(); bool success = processor.ProcessPayment(49.99m);

GetProcessor() can return any implementation. That is the core of interface polymorphism: the same interface reference can point to different implementations at runtime, and the method that actually runs is determined by the object's real type.

How the Runtime Dispatches Interface Calls

When you call a method through an interface reference, the CLR looks up the method in the object's type metadata and invokes the implementation associated with that type. This is a virtual dispatch, similar to what happens when you call a virtual method on a base class. The dispatch is not resolved at compile time because the compiler cannot know which implementation will be present when the code runs.

This runtime resolution has a small cost. Interface dispatch typically involves an extra indirection compared to a direct call on a sealed class. In most application code this cost is negligible, but in tight loops that call interface methods millions of times, the difference can become measurable. If profiling shows that interface dispatch is a bottleneck, you can store the instance in a local variable typed as the concrete class after a single cast, or restructure the hot path to avoid repeated interface calls.

Interface Polymorphism vs Abstract Class Inheritance

Both interfaces and abstract classes enable polymorphic behavior, but they differ in what they allow you to express. A class can implement many interfaces but can inherit from only one base class. Before C# 8, interfaces carry no implementation, whereas an abstract class can provide shared implementation for derived classes. C# 8 and later add default interface methods, but the primary role of an interface remains defining a contract rather than sharing implementation.

ConcernInterfaceAbstract Class
Multiple inheritanceA class can implement many interfacesA class can inherit from only one abstract class
ImplementationClassic interfaces have no implementation; C# 8+ may include default implementationsCan provide shared implementation
Member kindsPrimary member kinds: methods, properties, events, and indexersFull class member set, including fields and constructors
VersioningAdding a member can break implementers unless it has a default implementation (C# 8+)Adding a member can be given a default implementation

Choose an interface when the contract is the only thing that matters and implementations have nothing meaningful to share. Choose an abstract class when derived types share state or behavior that should be inherited. Many designs use both: an abstract class implements an interface and provides a partial implementation, leaving specific behavior to derived classes.

Common Mistakes with Interface Polymorphism

One frequent mistake is treating an interface as a way to force unrelated classes into the same shape without considering whether the contract is meaningful. If two classes implement the same interface but the caller must check the concrete type to behave correctly, the interface is not earning its place.

csharp
if (processor is CreditCardProcessor cc) { cc.ValidateCardNumber(); }

That kind of type check undermines polymorphism. If the caller needs to know the concrete type, the abstraction is leaking. A better design moves the varying behavior behind an interface member, so the caller can rely on the contract alone.

Another common mistake is trying to use the interface to expose internal details. In classic C# (before C# 8), interface members are public by default and cannot have access modifiers; C# 8 and later allow access modifiers and default implementations, but an interface should still describe a contract for callers rather than internal implementation details. Keep the interface focused on what callers actually need.

Where Interface Polymorphism Shines in Real Code

The most valuable use of interface polymorphism is dependency injection and testing. When a service depends on an interface rather than a concrete class, you can substitute a fake implementation in tests without changing the service's code. This is only possible because the service treats the dependency polymorphically.

csharp
public class OrderService { private readonly IPaymentProcessor _processor; public OrderService(IPaymentProcessor processor) { _processor = processor; } public bool PlaceOrder(Order order) { return _processor.ProcessPayment(order.Total); } }

In production you inject a real processor; in tests you inject a fake one. The service does not care which one it receives. This is the same polymorphic mechanism as the earlier example, applied at the dependency boundary.

Maintainability Tradeoffs of Interface Polymorphism

Interface polymorphism improves maintainability when the set of implementations is expected to grow or change independently of the caller. Adding a new payment processor requires no change to the caller. However, adding a new abstract method to the interface forces every implementer to update unless the method has a default implementation (C# 8+). That is a real cost, and it grows with the number of implementations.

If you control all implementers and the interface is small, the cost is manageable. If the interface is large or implemented by external parties, consider whether a new interface should be introduced instead of extending the existing one. Interface segregation keeps contracts small and reduces the blast radius of changes.

Performance Considerations for Interface Dispatch

Interface dispatch is not free, but it is rarely the reason a system is slow. The extra indirection matters only in code that runs extremely frequently, such as a per-frame game loop or a high-throughput data pipeline. In those cases, you can often replace an interface call with a direct call after a single cast, or by using a generic method constrained to the interface, which allows the JIT to devirtualize the call in some scenarios.

Do not optimize interface dispatch preemptively. Measure first. If profiling shows that interface calls are a meaningful fraction of execution time, apply one of the techniques above. Otherwise, keep the interface because the maintainability benefit outweighs the tiny runtime cost.

Choosing the Right Abstraction Level

Interface polymorphism is most effective when the interface represents a stable contract and implementations vary in behavior. If implementations share substantial logic, an abstract class or a base class with virtual methods may serve better. If you only need one implementation and do not expect another, an interface adds indirection without immediate benefit, though it can still be justified if you anticipate future implementations or need test doubles.

The decision comes down to how the code will change. If new implementations are likely, interface polymorphism gives you a clean extension point. If the contract itself is likely to change frequently, keep the interface small and be prepared to update implementers. That tradeoff is inherent to interface-based design, and understanding it is more valuable than memorizing the syntax.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.