Home > Articles > Programming > C#

  • Print
  • + Share This
Like this article? We recommend


Interfaces are extremely useful in C# programming. It's no coincidence that interfaces are also pivotal entities in the Java world. Using an interface, you can define a small pocket of capabilities and then implement the interface in a class. An interface provides no code beyond a method name; the implementing class must provide the code designated in the interface. In this context, an interface defines a contract, which is fulfilled by the implementing class.

Once an interface is defined and used by client code, it's generally bad practice to change the interface. Instead, you should extend the interface hierarchy and define the new code there. This isn't always possible, though. For example, in some cases, the interface and client code may have to be changed.

It's possible to define version numbers for interfaces as markers for when the code was written. This is optional, of course. Another important consideration with interfaces is dynamically determining whether a given type supports a specific interface. Fortunately, C# includes some excellent tools for this purpose, in the form of the is and as keywords.

Finally, the need often arises to design against either interfaces or abstract base classes. While this choice may be a matter of style, an abstract base class may be a little more heavyweight in nature. An interface is typically a lightweight item of code.

  • + Share This
  • 🔖 Save To Your Account