Monday, April 20, 2015

Interface in C#

An interface looks like a class, but has no implementation. It can be a member of a namespace or a class and can contain signatures of methods, properties, events or indexers.That's why it is said to be less concrete, more abstract.At the time of execution, the runtime selects an implementation of the interface.

An interface can be created using the keyword interface.

Syntax

<access modifier> interface <interface name> 
{                                                                   
 //declare member                                          
}                                                                   

Access modifier of an interface is public or internal. By default it is internal. Hence if you don't mentioned any access modifier then it is considered as internal. But members of an interface are always public. You need not to prefix any access modifier for them.


    
    public interface IExample
    {
        //member property
        int BasicPay { get; set; }
        //.....

        //member function
        double GrossSalary();
    }

Implementing an interface is itself an art. By choosing the efficient way we can avoid the classic diamond problem. Or we can say C# remove this problem by design different way of interface implementation. There are two ways of implement an interface. Implicit and explicit.

Implicit implementation


    
    public class Example : IExample
    {
        private int _BasicPay;

        public int BasicPay
        {
            get
            {
                return _BasicPay;
            }
            set
            {
                _BasicPay = value;
            }
        }

        public double GrossSalary()
        {
            //some bussiness logic
            return _BasicPay * 4.5;
        }
    }

Explicit implementation

A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.


    public class Example : IExample
    {
        private int _BasicPay;

        int IExample.BasicPay
        {
            get
            {
                return _BasicPay;
            }
            set
            {
                _BasicPay = value;
            }
        }

        double IExample.GrossSalary()
        {
            //some bussiness logic
            return _BasicPay * 4.5;
        }
    }

Complete program

using System;

namespace InterfaceExample
{
    public interface IExample
    {
        //member property
        int BasicPay { get; set; }
        //.....

        //member function
        double GrossSalary();
    }


    public class Example : IExample
    {
        private int _BasicPay;

        int IExample.BasicPay
        {
            get
            {
                return _BasicPay;
            }
            set
            {
                _BasicPay = value;
            }
        }

        double IExample.GrossSalary()
        {
            //some bussiness logic
            return _BasicPay * 4.5;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.Clear();
            IExample example = new Example();
            Console.WriteLine("Enter basic salary : ");
            example.BasicPay = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Gross salary is : {0}", example.GrossSalary().ToString());
            Console.ReadLine();
        }
    }

}

Output

Enter basic salary :       
112000                        
Gross salary is : 504000


Happy Coding ..:)