C# Interface

C# Interfaces are similar to classes, but it only contains declarations for methods and properties. Interfaces can be considered as plugins to classes. The class that implements a particular interface is required to make an implementation for the members and methods of that interface. Let’s take a look at how we can define and use an interface on a class.

using System;
 
namespace InterfaceDemo
{
    interface ISample                     
    {                                     
        void ShowMessage(string message); 
    }                                     
 
    public class Sample : ISample                 
    {                                             
        public void ShowMessage(string message)   
        {                                         
            Console.WriteLine(message);           
        }                                         
    }                                             
 
    class Program
    {
        public static void Main()
        {
            Sample sample = new Sample();
 
            sample.ShowMessage("Implemented the ISample Interface!");
        }
    }
}

Example 1 – Using an Interface

Implemented the ISample Interface!

We defined an interface named ISample. As a naming convention, interfaces use Pascal Casing and all interfaces should start with letter I. We defined a method inside the interface  (line 7). You may notice that the method has no body. When defining methods inside an interface, you only need to write the header of the method. Also, note that methods and properties defined inside an interface cannot have access specifiers since they should always be accessed by implementing classes.

When a class needs to implement an interface, we use the same syntax as the  one we used for inheritance. The class that implemented the interface provide  the actual code for the members of that interface. As you can see in our Sample class, it provided an implementation for the ShowMessage() method of the ISample interface.

You are not limited to implementing just one interface for a class.

class Sample : ISample1, ISample2, ISample3
{
 //Implement all interfaces
}

You can supply as many interfaces as you want but make sure that the class that implement multiple interfaces will provide implementations for all the members of those interfaces. If a class is inheriting a base class and implementing interfaces at the same time, the base class should go first in the list before the interfaces.

class Sample : BaseClass, ISample1, ISample2
{
}

You can also use the is operator to check if a particular object implements an indicated interface.

Sample sample = new Sample();

if(sample is ISample)
{
   Console.WriteLine("sample implements the ISample Interface!");
}

Another thing to note is you cannot create instances of an interface because it has no constructor. The following code is illegal.

ISample sample = new ISample();

The following is an example of an interface that has a property.

interface ISample
{
   int Number { get; set; }
}

You do not provide any code for the getter and the setter. The class that will implement the interface will handle that.

class Sample : ISample
{
   private int number;
   
   public int Number
   {
      get { return number; }  
      set { number = value; } 
   }
}

Interfaces can even implement other interfaces. Consider the following example.

using System;
 
namespace InterfaceDemo2
{
    interface IBase
    {
        void BaseMethod();
    }
 
    interface ISample : IBase
    {
        void ShowMessage(string message);
    }
 
    public class Sample : ISample
    {
        public void ShowMessage(string message)
        {
            Console.WriteLine(message);
        }
 
        public void BaseMethod()
        {
            Console.WriteLine("Method from base interface!");
        }
    }
 
    class Program
    {
        public static void Main()
        {
            Sample sample = new Sample();
 
            sample.ShowMessage("Implemented the ISample Interface!");
            sample.BaseMethod();
        }
    }
}

Example 2 – Interface Which Implement Another Interface

You can see that even if the Sample class only implemented the ISample interface, it needs to implement all the members of IBase since ISample inherited from it.

Written by compitionpoint

Leave a Comment

Your email address will not be published. Required fields are marked *