C# Interfaces Introduction


This entry is part 1 of 8 in the series C# Interfaces

The post C# Introduction lists all of the C# series of posts we have at this site. A series is a group of a similar kind, and the series, if any, are listed at the top-right of the posts, inside a blue box.

We have another few posts on interfaces that are based on material in the book Illustrated C# 7, Fifth Edition by Daniel Solis and Cal Schrotenboer published by Apress. The first post is called C# Interfaces Illustrated.

An interface is a reference type that specifies a set of function members but does not implement them. That’s left to classes and structs that implement the interface.

An interface is a language construct that is similar to a class (in terms of syntax) but is fundamentally different. An interface is simply a declaration of the capabilities (or services) that a class should provide. An interface is a collection of public instance (that is, nonstatic) methods and properties that are grouped together to encapsulate specific functionality. An interface can only declare methods and properties, but not fields (because fields are about implementation detail). After an interface has been defined, you can implement it in a class with the syntax of a colon. This means that the class will then support all of the properties and members specified by the interface.

Why?

Interfaces help in building loosely coupled applications. We reduce the coupling between two classes by putting an interface between them. This way, if one of these classes changes, it will have no impact on the class that is dependent on that (as long as the interface is kept the same). To use Mosh Hamedani’s example in his Udemy.com course, suppose you have an OrderProcessor that is dependent on TaxCalculator to figure out how much tax to charge. You could make the TaxCalculator into an interface ITaxCalculator and now the OrderProcessor is only dependent on the interface, not the implementation.

One of the common misconceptions about interfaces is that they are used to implement multiple inheritance in C#. This is fundamentally wrong, yet many books and videos make such a false claim. Interfaces are not used to implement multiple inheritance.

Below is a list of important things to know about an interface in C# language.

  • An interface is a contract between a service provider (server) and the user of the service (client).
  • An interface describes what is in a class and a class is the how each method is implemented.
  • An interface has public instance (nonstatic) methods and properties that are grouped together to encapsulate specific functionality.
  • You cannot instantiate an interface.
  • Interfaces are not classes and thus do not inherit from System.Object
  • Prefixed with a capital I, normally
  • A class can support multiple interfaces and multiple classes can support the same interface.
  • Access modifiers public and internal are used in the same way with interfaces
        public interface ITaxCalculator
        {
            int Calculate();
        }

Maintenance

If we want to make changes in our application, we can write more classes and add them to the project. We do not need to recompile existing code. How is this possible?

Series NavigationC# Disposable Objects >>