Understanding Interfaces in C#

I have worked with a large number of developers throughout my career and I can say that one area that causes a lot of confusion and can take a lot of time to fully understand is the concept of interfaces.

I believe the main reason for this is a large number of online tutorials overcomplicating the subject with very academic descriptions and complex examples. Interfaces are in general a simple concept; they can be easily understood with a good and simple example.

When describing what interfaces are and when they are useful I refer to the USB (Universal serial Bus) example.

USB is so wide spread that its common on all manner of electronic devices. This means it’s easy to explain to developers who use computers on a daily basis.

If we have 2 devices that inherit the USB interface, let’s say a desktop PC and a laptop. As they have USB ports, I can connect a USB device such as a memory stick with these devices. However, my Headphones do not inherit the USB interface, but rather the audio jack interface. So this means I cannot utilize USB with this device.

So how does this work in relation to C#? let’s look at some example code.

We have 2 classes, Desktop_PC and Laptop_PC which both inherit the interface IUSB.

    public interface IUSB
    {
        bool USB_Connector { get; set; }
        bool USB_Power { get; set; }
        List<Byte> USB_Data_Transfer();
    }

Here is our IUSB interface, notice how we don’t have any accessibility modifiers, it’s just the return type and name. Also, it’s a good conversion to name your interfaces with a Capital I at the beginning, Hence IUSB.

Now, let’s have a look at our desktop_PC and Laptop_PC classes that inherit the USB Interface.

    public class Desktop_PC : IUSB
    {
        public string Manufacturer { get; set; }
        public int CaseHeight { get; set; }
        public bool USB_Connector { get; set; }
        public bool USB_Power { get; set; }
        public List<Byte> USB_Data_Transfer()
        {
            // USB_Data_Transfer Method Logic...
        }
    }

 

    public class Laptop_PC : IUSB
    {
        public int ScreenSize { get; set; }
        public bool USB_Connector { get; set; }
        public bool USB_Power { get; set; }
        public List<Byte> USB_Data_Transfer()
        {
            // USB_Data_Transfer Method Logic...
        }
    }

Notice that as long as a class that inherits an interface and declares the members of the interface they can have other members that are unique to the class, so Laptop_PC has ScreenSize which is unique to the laptop and the desktop has CaseHeight which is unique to the desktop.

Let’s have a look at the class below. This is the MemoryStick class which in its non-default constructor takes any object of that inherits the IUSB interface, so this means I could pass Desktop_PC or Laptop_PC, or any class that inherits the IUSB interface. This is the main advantage of Interfaces; you can pass any object that inherits the interface. This could be a TV, Mouse, Phone, anything that has USB.

    public class MemoryStick
    {
        public double MemorySize { get; set; }
        public List<Byte> Data_Transfer { get; set; }

        public MemoryStick()
        {
            Data_Transfer = new List<byte>();
        }

        public MemoryStick(IUSB USBDevice) : this()
        {
            USBDevice.USB_Power = true;
            USBDevice.USB_Connector = true;

            foreach(var data in USBDevice.USB_Data_Transfer())
            {
                // start downloaning data to the device....
            }            
        }
    }

It’s also important to know that in C# a class can inherit multiple interfaces. The example below shows how I can modify Laptop_PC with an Audio jack interface.

    public class Laptop_PC : IUSB, IAudioJack
    {
        public int ScreenSize { get; set; }
        public bool USB_Connector { get; set; }
        public bool USB_Power { get; set; }
        public List<Byte> USB_Data_Transfer()
        {
            // USB_Data_Transfer Method Logic...
        }

        public bool Audio_Connector { get; set; }
        public List<Byte> Audio_Transfer { get; set; }
    }

    public interface IAudioJack
    {
        bool Audio_Connector { get; set; }
    }

 Interfaces allow you to create more elegant code with much better reuse and readability, try utilising interfaces in your project where you have similar class objects, refactor these using an interface.


JGilmartin Profile Image

JGilmartin

Technical Architect at Pinewood Technologies

Rating: 2890

C# Expert

Offline


Tutorial Statistics
  • Views: 441
  • Comments: 0
  • Author: JGilmartin (2890)
  • Date: 22/11/2016 22:50
Tags
C#

© 2016 - 2018 - IntermittentBug