[C# Basics] What is an interface?

catgirl

Administrator
Staff member
Administrator
May 28, 2020
155
28
22
United States
envyforums.net
C# is a programming language that runs on top of the Microsoft .NET Runtime (there are alternatives such as Mono and the new dotnet core runtime however they will all be merged into .NET 5 late 2020) and it is one of the most popular programming languages. It has strong support for Microsoft SQL Server which makes it a good programming language for business data-driven applications. This tutorial post discusses what C# classes are, how to create them, and why you should use them.

What is an interface?

Sometimes you will need to ensure that a group of classes you create follows certain requirements for example it must have certain properties, methods, indexers, or events. To do this you will create an interface and the class must implement or it the application will not successfully build. (In C# 8 and above an interface can have default implementations, when a default implementation is defined, the application will build successfully and use the default implementation if the class has not defined its own implementation).

How to create an interface?

To create an interface, you’ll use code similar to that of a class declaration. It’s the proper convention to start an interface name with the letter I (as in Icecream). Unlike a class, in an interface you only write the declaration, not the implementation (unless you are using C# 8’s default implementation’s functionality). Take a look at the following code example to gain a better understanding of how interfaces work.

C#:
interface ICake
{
  // properties
  public string Flavor { get; set; }
  public int AmountOfIcing { get; set; }
  public int NumberOfMinutesToBake { get; set; }
 
  // methods
  void PutItInTheOven();
  void BakeAsFastAsYouCan();
}

class BirthdayCake : ICake
{
  // implemented properties
  public string Flavor { get; set; }
  public int AmountOfIcing { get; set; }
  public int NumberOfMinutesToBake { get; set; }

  // constructor
  public Cake(flavor, amountOfIcing, numberOfMinutesToBake)
  {
    Flavor = falvor;
    AmountOfIcing = amountOfIcing;
    NumberOfMinutesToBake = numberOfMinutesToBake;
  }

  // implemented methods
  void PutItInTheOven()
  {
    Console.WriteLine("Cake was put in the oven.");
  }

  void BakeAsFastAsYouCan()
  {
    Console.WriteLine("Cake is being baked as fast as possible.");
  }
 
  // Class specific methods
  void SingASong()
  {
      SingSong(); // Method will sing happy birthday
  }
}
You'll notice I added a method specific to my class's functionality called SingASong, which will sing happy birthday over the computer's speakers. I still implemented all the requirements of a cake, but can add my own class specific methods. There might be other types of cakes that do not require a song such as a Wedding Cake or Graduation Cake so the interface does not require this method to be implemented across all classes which implement the interface.

Why you should use interfaces?

Interfaces allow you to ensure a group of classes all implement a set of behavior and builds upon the behavior of classes by building a well designed application with consistent behavior. When you have several similar classes you can get into a similar situation where objects have inconsistent behavior, while it's a bit easier to track down, interfaces avoid this issue from occurring in the first place.