Interface In C#.NET In a Simple Way - Part One

In this post, we will discuss Interface in C#.NET and I will explain the use of interface with an example in a simple way. I hope this will be very helpful for developing real-time projects for freshers as well as experienced people.
 
What is the need of Interface? Why use Interface?
  • It is used to Decouple the application.
  • It is very useful for Dependency Injection.
  • It is useful for Test isolation and Mocking.
  • It is used for Extensibility
What is Interface?
  • An interface can contain only the declaration of properties, methods, events, indexers etc.
  • Interface member does not have the definition.
  • Interface member is by default Public. We can not provide access modifier to the interface member.
  • We can inherit more than one interface in a class. Once we inherit interface in a class, we must provide definition to the interface member.
  • An interface cannot contain fields, operators, instance constructor etc.
In order to work with the interface, we use "Interface" keyword to create an interface. We can provide "I" as  an interface naming prefix for interface names like IVendor, ICustomer etc.

Syntax
  1. interface interface_name  
  2.    {   
  3.        // interface member declaration  
  4.    }  
Example

Create an interface which has a name like IVendor as follows.
  1. interface IVendor  
  2.    {  
  3.        bool AllowAccessToUser { getset; }  
  4.        string GetVendorName(int vendorId);  
  5.    }  
Now, we need to create a class to implement interface member. Create a class which has a name like Vendor as follows. 
  1. public class Vendor: IVendor  
  2.    {  
  3.        public bool AllowAccessToUser { getset; }  
  4.        public string GetVendorName(int vendorId)  
  5.        {   
  6.            string vendorName =string.Empty;  
  7.            switch (vendorId)  
  8.            {   
  9.                case 1:  
  10.                    vendorName = "TATA";  
  11.                    AllowAccessToUser = true;  
  12.                    break;  
  13.                case 2:  
  14.                    vendorName = "TCS";  
  15.                    AllowAccessToUser = true;  
  16.                    break;  
  17.                default:  
  18.                    vendorName = "NULL";  
  19.                    AllowAccessToUser = false;  
  20.                    break;  
  21.            }  
  22.            return vendorName;                  
  23.        }  
  24.    }  
We can call the vendor class member by creating an instance of vendor class from main method. Define main method to access the vendor detail. There is one method, "GetVendorName()", based on vendorId as shown in the above example. In the same way we have created one property for user permission. Based on the vendor we can provide the access to the user. 
  1. class Program  
  2.     {         
  3.         static void Main(string[] args)  
  4.         {  
  5.             Vendor vendor = new Vendor();             
  6.             Console.WriteLine("Vendor name is {0}, Allow access to user is {1}", vendor.GetVendorName(1),vendor.AllowAccessToUser);  
  7.             Console.WriteLine("Vendor name is {0}, Allow access to user is {1}", vendor.GetVendorName(4), vendor.AllowAccessToUser);  
  8.             Console.ReadLine();  
  9.         }  
  10.     }  
Output
 
How to implement more than one interface in a class. 

Create two interface names as "IVendor" and "IUserPermission". Copy the above example and paste it into a separate class. Create one more interface "IUserPermission" interface having one property and remove the property used in IVendor interface. Paste into the newly-created interface; i.e., "IUserPermission". There are no major changes.
 
This is the example to understand how to implement more than one interface in a class.
  1. interface IVendor  
  2.    {         
  3.        string GetVendorName(int vendorId);  
  4.    }  
  5.    interface IUserPermission  
  6.    {  
  7.        bool AllowAccessToUser { getset; }  
  8.    }  
  9.    public class Vendor : IVendor, IUserPermission  
  10.    {  
  11.        public bool AllowAccessToUser { getset; }  
  12.        public string GetVendorName(int vendorId)  
  13.        {   
  14.            string vendorName =string.Empty;  
  15.            switch (vendorId)  
  16.            {   
  17.                case 1:  
  18.                    vendorName = "TATA";  
  19.                    AllowAccessToUser = true;  
  20.                    break;  
  21.                case 2:  
  22.                    vendorName = "TCS";  
  23.                    AllowAccessToUser = true;  
  24.                    break;  
  25.                default:  
  26.                    vendorName = "NULL";  
  27.                    AllowAccessToUser = false;  
  28.                    break;  
  29.            }  
  30.            return vendorName;                  
  31.        }  
  32.    }  
  33.    class Program  
  34.    {         
  35.        static void Main(string[] args)  
  36.        {  
  37.            Vendor vendor = new Vendor();             
  38.            Console.WriteLine("Vendor name is {0}, Allow access to user is {1}", vendor.GetVendorName(1),vendor.AllowAccessToUser);  
  39.            Console.WriteLine("Vendor name is {0}, Allow access to user is {1}", vendor.GetVendorName(4), vendor.AllowAccessToUser);  
  40.            Console.ReadLine();  
  41.        }  
  42.    }  
We will discuss the type of interface and real-time use of interface in part two of this series. more information click on following link.
interface in csharp in a simple way part two
I hope you understood the basic concept of interface.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now