Home
Blog
Should Interfaces Be Preferred in C#
Updated
Dot Net Perls

Should Interfaces Be Preferred in C#

There are some things about C# that are confusing, even for developers who have known the language for a while. For example, there are many ways to have a type share parts of itself with another type. We have inheritance, abstract classes, and interfaces.

When should we use a normal class, and then derive another class from it? And when should an abstract class be used? Interfaces meanwhile are not a class at all, but allow us to treat different classes as though they are the same, through the interface.

After working with languages that do not support inheritance at all, I suggest just using interfaces:

Interfaces allow us to unify 2 different types and share code that acts on those types through the interface.
Interfaces avoid complicated issues with base constructors.
Interfaces are used throughout the built-in types in .NET, so developers can figure out how to use them without much trouble.

I am not against using language features to improve program quality, but inheritance often seems to result in excess complexity—to understand one class, you need to understand all the base classes. Instead, with interfaces, our code is flatter and more local to the class. Class derivation can be useful, but in my view, interfaces usually are a better choice.

Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
An RSS feed is available for this blog.
Home
Changes
© 2007-2025 Sam Allen