Implicit and Explicit Interface with Examples

Introduction

In this article we are going to discuss about Implicit and Explicit Interface with Examples, in previous article we are learn interface with example

What is Interface In c# ?

  1. An interface in C# is usually used to create loosely-coupled and contract-based designs.
  2. It can contain signatures (declarations) of the Methods, Properties, Indexers and Events.
  3. An interface can inherit one or more interfaces, in other words it supports multiple inheritance whereas classes don’t.
  4. A class that implements an interface can explicitly implement members of that interface
  5. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.

Purposes of Interfaces :

  1. Create loosely coupled software.
  2. Facilitate reuse of software.
  3. Support design by contract (an implementer must provide the entire interface).
  4. Allow objects to interact easily.
  5. Allow for pluggable software.

Implicit interface implementation

This is the most regular or obvious way to implement members of an interface. Here we don’t specify the interface name of the members and implement implicitly. The method can be declared at any interface (s) the class implements.

using System;
namespace OppsConcept
{
     class Program
     {
        static void Main(string[] args)
        {
            Interface1 obj1 = new TestClass();
            obj1.Method1();
        }
     }

    interface Interface1
    {
        void Method1();
    }

The call of the method is also not different. Just create an object of the class and invoke it.

     class Program
     {
        static void Main(string[] args)
        {
            TestClass obj1 = new TestClass();
            obj1.Method1();
            Console.ReadKey();
        }
     }

Explicit interface implementation :

This is another way to implement members of an interface. Here we need to specify the interface name of the members. The following example explains that.

    interface Interface1
    {
        void Method1();
    }
    interface Interface2
    {
        void Method1();
    }

    class TestClass : Interface1, Interface2
    {
        void Interface1.Method1()
        {
            Console.WriteLine("Explicit Interface Implementation call interface1 method");          
        }

        void Interface2.Method1()
        {
            Console.WriteLine("Explicit Interface Implementation call interface2 method");
        }
    }

Explicit interface must be used when multiple interface inherit with same method name in this case we implement method with using interface name before method name.

Now run console application and see output for above program

     class Program
     {
        static void Main(string[] args)
        {
            Interface1 obj1 = new TestClass();
            obj1.Method1();

            Interface2 obj2 = new TestClass();
            obj2.Method1();

            Console.ReadKey();
        }
     }

 Output :

Implicit and Explicit Interface Examples

Also Learn More Tutorial :

1 Comment

Leave a Reply

Your email address will not be published.