C# Tutorial

C# String

C# Array

C# Flow Control

C# Class and Object

C# Inheritance

C# Interface

C# Collection

C# Generic

C# File I/O

C# Delegate and Event

C# Exception

C# Process and Thread

C# ADO.NET Database Operations

Implement Interfaces In C#: Explicit Implementation And Implicit Implementation Of Interfaces

In this tutorial, we will explore how to implement interfaces in C# using both explicit and implicit implementations. Interfaces define a contract that classes or structs can implement to provide certain functionality.

  • Implicit Implementation of Interfaces

An implicit implementation is when a class implements an interface without specifying the interface name. This is the most common way to implement interfaces.

Here's an example of a simple IDisplayable interface and a Person class that implicitly implements the interface:

public interface IDisplayable
{
    string DisplayName();
}

public class Person : IDisplayable
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string DisplayName()
    {
        return $"{FirstName} {LastName}";
    }
}

In this example, the Person class implicitly implements the IDisplayable interface by providing a DisplayName method with the same signature as the one defined in the interface.

  • Explicit Implementation of Interfaces

An explicit implementation is when a class implements an interface by specifying the interface name in the method signature. This is useful when a class needs to implement multiple interfaces with conflicting method signatures, or when you want to hide the interface method from the public API of the class.

Here's an example of a Person class that explicitly implements the IDisplayable interface:

public class Person : IDisplayable
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    string IDisplayable.DisplayName()
    {
        return $"{FirstName} {LastName}";
    }
}

In this example, the DisplayName method is marked with the IDisplayable interface name, indicating an explicit implementation. When a method is explicitly implemented, it is not accessible through a reference to the class itself but only through a reference to the interface.

  • Comparing Implicit and Explicit Implementations

The main difference between implicit and explicit implementations lies in their accessibility and usage.

  • Implicit implementations are part of the public API of the class and can be called both through a reference to the class and a reference to the interface.
  • Explicit implementations are hidden from the public API of the class and can only be called through a reference to the interface.

Here's an example that demonstrates the difference in usage:

Person person = new Person { FirstName = "John", LastName = "Doe" };

// Implicit implementation
string displayName = person.DisplayName(); // This works

IDisplayable displayable = person;
displayName = displayable.DisplayName(); // This works too

// Explicit implementation
displayName = person.DisplayName(); // This will not compile

displayable = person;
displayName = displayable.DisplayName(); // This works

This tutorial demonstrates how to implement interfaces in C# using both explicit and implicit implementations. Implicit implementations are more common and provide a simpler syntax, while explicit implementations are useful for resolving conflicts between multiple interfaces or hiding interface methods from the public API of a class.

  1. Explicit interface implementation in C#

    Explicit interface implementation allows a class to implement an interface method explicitly. Here's an example:

    using System;
    
    interface IExample
    {
        void Display();
    }
    
    class MyClass : IExample
    {
        void IExample.Display()
        {
            Console.WriteLine("Explicit interface implementation");
        }
    }
    
    class Program
    {
        static void Main()
        {
            MyClass myObject = new MyClass();
            ((IExample)myObject).Display(); // Accessing the method explicitly
    
            Console.ReadLine();
        }
    }
    
  2. Implicit interface implementation in C#

    Implicit interface implementation is the common way of implementing interfaces in C#:

    using System;
    
    interface IExample
    {
        void Display();
    }
    
    class MyClass : IExample
    {
        public void Display()
        {
            Console.WriteLine("Implicit interface implementation");
        }
    }
    
    class Program
    {
        static void Main()
        {
            MyClass myObject = new MyClass();
            myObject.Display(); // Accessing the method implicitly
    
            Console.ReadLine();
        }
    }
    
  3. How to implement interfaces in C#

    To implement an interface, use the : syntax after the class declaration. Example:

    interface IExample
    {
        void Display();
    }
    
    class MyClass : IExample
    {
        public void Display()
        {
            Console.WriteLine("Interface implementation");
        }
    }
    
  4. Multiple interface implementation in C#

    A class can implement multiple interfaces by separating them with commas. Example:

    interface IFirstInterface
    {
        void MethodA();
    }
    
    interface ISecondInterface
    {
        void MethodB();
    }
    
    class MultipleInterfaces : IFirstInterface, ISecondInterface
    {
        public void MethodA() { /* Implementation */ }
        public void MethodB() { /* Implementation */ }
    }
    
  5. Interface inheritance in C#

    Interfaces can inherit from other interfaces using the : syntax. Example:

    interface IBase
    {
        void BaseMethod();
    }
    
    interface IDerived : IBase
    {
        void DerivedMethod();
    }
    
  6. Using interfaces for polymorphism in C#

    Interfaces enable polymorphism, allowing objects of different classes to be treated uniformly if they implement the same interface.

    interface IShape
    {
        void Draw();
    }
    
    class Circle : IShape
    {
        public void Draw()
        {
            Console.WriteLine("Drawing a circle");
        }
    }
    
    class Square : IShape
    {
        public void Draw()
        {
            Console.WriteLine("Drawing a square");
        }
    }