Using a C# Interface to Create Reusable Code

c sharp interfaceComputer programs can be a beast to manage and reuse if we let them. While object-oriented programming (OOP) lets us build flexible, maintainable, and reusable code, it only gets us half way. C# Interfaces complete the job by offering an easily manageable component-based design that makes code reuse much more accessible. You might never need to use them, but you will find them useful on occasion. Interfaces exist to allow your C# classes inherent from multiple parents. This enables your programs to utilize functionality they may not otherwise have. They also provide a standardized, universal structure to your class properties and methods. Understanding how interfaces and classes interact is the most difficult part of learning how to use interfaces in C#.

Getting started with C#? Learn the language at Udemy

What’s an Interface?

Structurally, interfaces are reference C# objects with no class member implementation. They do nothing but define their properties and methods. You cannot create objects of them either. Your classes must inherent their components in order to use the. Basically, interfaces exist so that other classes can interact with other classes and interfaces.

Like with most other components of OOP, C# interfaces are often better understood in terms of real world objects. Take for example, a pencil. While there are many kinds of pencils in the world, they all carry the same basic features. They all interact with other objects in similar ways as well. We never have to know how any particular pencil actually works. We only have to know how to use their interfaces.

The following code illustrates how to define a C# interface:

public interface IPencil
void Write();
bool IsSharp { get; }

While it isn’t necessary, common convention states that all interface names begin with a capitol ‘I’. Notice that I did not include a scope identifier for either the Write() method or the IsSharp property. This is because all interface members must be public by default.

Learn how to work with C# objects and classes today

Why Use Interfaces?

You might never ever have to use interfaces in your C# programs. You can implement similar features using either abstract or standard C# classes. However, interfaces make it easier to distribute your code. You never had to learn how to use each and every type of pencil separately. You only had to learn how to use one type of pencil to know how to use them all. The same goes with using C# classes that implement interfaces. You just have to know the properties and methods defined by the interface to use any C# program or class that uses it.

While you can create this usability with properly constructed C# classes, you run the risk of breaking things if you need to change pieces of your code. This may not matter to small projects, but code reuse breaks down as our non-interface based project mature and expand in complexity.

On the other hand, interface-based development can handle most code changes without incident provided we store all the more consistent features in their own interface.  When we see an interface, we know there is a consistent underlying implementation in place for us to use.

Interfaces also work best when we need to define how two or more classes should interact.

Learn how to create reusable C# code at Udemy

Implementing C# interfaces

Since interfaces only define their members, we have to write their implementations elsewhere. This is done inside any class that uses our interfaces. For instance, the following C# class implements our IPencil interface.

class BondPencil : IPencil

public BondPencil(Pencil pencil)
m_pencil = pencil;

private Pencil m_pencil;

public string Message
get { return m_pencil.Message; }
set { m_pencil.Message = value; }

public bool IsSharp
get { return m_pencil.IsSharp; }

public void Write()
if (!IsSharp) Console.WriteLine("Blows up killing the bad guy!");

public void OnSharpened()


You enable your classes to use an interface by including the name of the interface right after your class name. The colon, ‘:’, separates the class name from the interface, and is called the interface operator. It means “this class uses the interface that follows”.  Note that our BondPencil class defines how our IPencil class members function.

C# classes can implement any number of interfaces. You just have to list each interface after the colon separated by commas. This is how C# classes have multiple parent classes. By design, C# classes can only have one official parent class, but they can have as many interfaces as they need. We use these interfaces to create our desired scalable programs.

For example, our BondPencil is great for writing and decrypting secret notes, but not for spying underwater. We would then want our BondPencil to provide a way for us to breathe. We can do this and include a one shot gun as well by using the following code:

class BondPencil : IPencil, IUnderwaterBreathingApparatus, IOneShotGun

Advanced C# Interface Techniques

If several classes implement an interface, then using them through the interface works best if we want to ensure our code does not break if someone messes with our code or when our needs change. For instance, we may have different pencils for different tasks. However, we may want to know which class we are actually using. For instance, we don’t want to blow up the world with our BondPencil. We can do this using three different methods.

  1. The first method uses the ‘is’ keyword which checks the identity of our class:

if (p is BondPencil) return; //RUN‼

  1. The ‘as’ keyword returns a reference to our test class if there is a match.

if (null != p as BondPencil) return; // NOTIFY THE MEDIA!

  1. Finally, we can cast our test class into itself if we know its identity.

(BondPencil) p

Casting our interfaced class gives us access to all the non-interface functionality, but the cast will return an InvalidCastException if the cast fails.

Either way, C# interfaces make your programs highly manageable, reusable, and flexible. You have to develop them properly during the early stages of your development cycle, but the end results make them worth it. Consistent interfaces make your programs stable and robust while allowing other developers understand your code.