Part Two: Interfaces in C# – Implementing the interface

In Part One we discussed what an interface is. We also outlined what our interface could look like. In Part Two we are going to create a class that will implement the interface and do something meaningful with it.

To recap this is what our interface looks like in code

public interface ICompanyAbcContract
{
  string EmployeeName { get; set; }
  string HolidayEntitlement { get; set;}
  int WorkingHours { get; set; }
  string JobDescription { get; set; }
  string Responsibilities { get; set; }

  void GenerateContract(); 
}

 

Lets see what this looks like in Visual Studio.

 ICompanyAbcContract in Visual Studio

 OK, that looks simple and straightforward. Lets implement this class with a scenario

I want you to create an application for me that the HR team can use to create employee contracts.

HR Director

OK, lets get started. He wants an application, but he hasn’t given us any details about how he wants his users to interact with the application. Let’s keep it simple then, we’ll write a console application. There’s a few things we need to do to set ourselves up properly.

  • Add a new Console Application project to our solution in Visual Studio. I’ll call this project ConsoleEmployeeContract.
  • Add a reference to the new Console Application for the project that contains our interface.
  • Create a class called ContractManagement in our ConsoleEmployeeContract project.
  • Add a Using reference for our interface to the ContractManagement class

You can see how this looks in Visual Studio below.


Before we go any further, lets have a more detailed look at what we have

  • InterfacesTutorial Project – this is the class project that contains our Interface, ICompanyABCContract. This is defined in the ICompanyABCContract.cs file.
  • ConsoleEmployeeContract Project – this is our Console Project. It will be our application for the HR Director.
  • ConsoleEmployeeContract has a reference to the InterfacesTutorial project.
  • ConsoleEmployeeContract contains two classes, Program.cs which is our application entry point and ContractManagement.cs which is where we will implement our interface.

The next step is to implement our interface. Lets open our ContractManagement.cs file in the Visual Studio editor.

 

To have our class implement our interface,  we simply add the name of the interface to the public class ContractManagement statement. Note that it looks like we are inheriting from the interface. The syntax for implementing or inheriting is the same in C#. You can see how this might look confusing. I like to use ‘I‘ as a prefix to my interface classes so I can clearly see that it is an interface (ICompanyAbcContract). There’s a lot of discussion around dropping things like this from naming conventions, but this works for me, so I suggest doing something that works for you.

 

In Part One we learnt that we have to implement all the members of the interface. If you have the amazing ReSharper tool then it will automatically implement them for you. Either way, you need to implement all the members at this point.

 

The GenerateContract() method is what is going to do the actual generation of the contract. To keep this simple, our console application will output the contract details to the console screen. Obviously in a real world scenario, you would be implementing something more complex in this method, such as creating a word document containing the employee contract details, storing in a document management system, printing the contract out etc. The principals of what we are doing here are the same. Here is some code to output the contract to the console screen, using information set in the properties that are implemented.

 

We now have a class that has successfully implemented all the members of the interface! The GenerateContract() method has been populated with code to print the current contract to the screen. All we have to do now is actually write our application code to use it! I’ve written some code in to the Program.cs file to demonstrate how this could work. The code generates two contracts, one for each of the new employees in our scenario, Margaret and Fred. We create a new ContractManagement object and populate the details for each employee. We then call the GenerateContract() method on each of the ContractManagement objects. Note that our Program class also requires a Using statement for the original interface.

 

 If we execute the program, then we see the following result

 

 

So there we have it! We have a console application that is implementing our interface. At this point, you may be asking “why did we bother using an interface here?” We could have coded all of this inside of the ContractManagement class and not bothered with the interface at all (or even just done everything in the Program.cs file). All we have here is a one to one mapping; 1 interface to 1 implementer.

Its a fair point. Don’t worry! In Part Three I’ll demonstrate how starting out with an interface is going to make for an easier future!