Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Explicit and implicit interface implementation in C#

Explicit interface implantation

With explicit interface implementations you can only access them when treating it as that interface.

i.e  ISedanCar.GetCarDetails(){};

Why explicit interface implantation?

If a class implements two interfaces (Multiple inheritance) and both interfaces with the same method signatures and different implementations, then you must implement explicitly.

Implicit interface implantation

Implicit is when you define your interface via a member on your class or normal class member implementation without interface name prefix.

Note:

  • There is no need to specify public/private/protected with explicit implementations since it will always be public as interfaces cannot have non-public members.
  • Interface's methods are not overridden, they are implemented, in case if you are trying child class as abstract class remember it. 

Explicit and implicit interface implementation examples

Interfaces

  interface ICar
    {
        void GetCarDetails();
    }
    interface ISedanCar
    {
        void GetCarDetails();
    }
    interface ISuvCar
    {
        void GetCarDetails();
    }

 

Implicit interface implantation example

//implicit implementation  
    public class BasicCar : ICar
    {
        //implicit         public void GetCarDetails()
        {
            Console.WriteLine("Details: Basic car features");
        }
    }  


//Hot it works?
    class Program
    {
        static void Main(string[] args)
        {
            var mycar = new BasicCar();
            mycar.GetCarDetails();
            ICar icar = new BasicCar();
            icar.GetCarDetails();
        }
    }


Output:

Details: Basic car features
Details: Basic car features

Implicit and Explicit implantation with single interface example

 //implicit and explicit implementation 
    public class Car : ISedanCar
    {
        //implicit implementation
        public void GetCarDetails()
        {
            Console.WriteLine("Details: common car features");
        }
 
        //explicit interface
        void ISedanCar.GetCarDetails()
        {
            Console.WriteLine("Details: sedan car features");
        }
    }

 

//How it works? 
    class Program
    {
        static void Main(string[] args)
        {
            Car mycar = new Car();
            mycar.GetCarDetails();
            ISedanCar mySedanCar=new Car();
            mySedanCar.GetCarDetails();
        }
    }

Output:

Details: common car features
Details: sedan car features

Explicit implantation with two interfaces (multiple inheritance) example

  //explicit implementation with multiple interface 
    public class LatestCar : ISedanCar, ISuvCar
    {
        //explicit interface implementation
        void ISedanCar.GetCarDetails()
        {
            Console.WriteLine("Details: sedan car features");
        }
 
        //explicit interface implementation
        void ISuvCar.GetCarDetails()
        {
            Console.WriteLine("Details: suv car features");
        }
    }


   //Hot it works with class instance?
    class Program
    {
        static void Main(string[] args)
        {
            var myCar = new LatestCar();
           //Showing compile time error with following
            myCar.GetCarDetails();
        }
    }


  //Hot it works with interface references?
    class Program
    {
        static void Main(string[] args)
        {
            ISedanCar mySedanCar = new LatestCar();
            mySedanCar.GetCarDetails();
            ISuvCar mySuvCar = new LatestCar();
            mySuvCar.GetCarDetails();
        }
    }


Output:

Details: sedan car features
Details: suv car features

Invalid entry,please enter valid data.

Loading