Implicit and Explicit Interface Implementation in C#

 Attention programmers! Are you looking to create loosely-coupled and contract-based designs in C#? Look no further than the interface! With signatures for methods, properties, indexers, and events, an interface allows for implementation in a separate class. And the best part? C# does not support "multiple inheritances" (a class can only inherit from one base class). However, it can be achieved with interfaces, because the class can implement multiple interfaces!

But wait, there's more! Implementing interface members implicitly is the most common method, allowing for flexibility in where the method is declared. Don't miss out on the benefits of using interfaces in your C# programming. Try it out today!

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.

interface IMyInterface  
{  
    void MyMethod();  
}  
class MyClass: IMyInterface  
{  
    public void MyMethod()  
    {  
        Console.WriteLine("Implicit Interface Implementation");  
    }  
}  


Calling the method is also not different. Just create an object of the class and invoke it.
class Program  
{  
    static void Main(string[] args)  
    {  
        MyClass obj = new MyClass();  
        obj.MyMethod(); //Way to call implicitely implemented method  
    }  
 
}


Explicit interface implementation


Looking for a way to avoid naming conflicts in C# when implementing interface members? Look no further than explicit interface implementation! By using the interface name and a dot operator before the method, property, or indexer name, you can ensure that your code is unambiguous and free of errors.

This method of implementation is particularly useful when a class implements multiple interfaces with members of the same name. With explicit interface implementation, you can specify which interface the member belongs to, ensuring that your code is clear and easy to understand.

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 IMyInterface  
{  
    void MyMethod();  
}  
class MyClass: IMyInterface  
{  
    public void IMyInterface.MyMethod()  
    {  
        Console.WriteLine("Explicit Interface Implementation");  
    }  
}

The constraint with explicit implementation is that an explicitly implemented member cannot be accessed using a class instance, but only through an instance of the interface. 
Please have a look at the example below.

class Program  
{  
    static void Main(string[] args)  
    {  
    IMyInterface obj2 = new MyClass();  
        obj2.MyMethod();  
    }  
}


We hope our article has been informative and helpful for you! We value your opinion and would love to hear your thoughts. Do you have any comments, questions, or feedback about the article? We'd love to know!

Your feedback is incredibly important to us as it helps us to improve our content and provides you with the best possible experience. So don't be shy, share your thoughts with us and let us know what you think. We can't wait to hear from you!


Comments

Popular posts from this blog

Demonstration: Serializing Objects as JSON in C# using JsonSerializer and JsonConvert

Common keyword terms and concepts in C# programming