Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit and Explicit implementation of interface

Tags:

c#

.net

interface

While working on a upgrade i happened to come across a code like this.

interface ICustomization
    {
        IMMColumnsDefinition GetColumnsDefinition();
    }

    class Customization : ICustomization
    {
        private readonly ColumnDefinition _columnDefinition;

        //More code here.

        public ColumnsDefinition GetColumnsDefinition()
        {
            return _columnDefinition;
        }

        ColumnsDefinition ICustomization.GetColumnsDefinition()  //redundant
        {
            return GetColumnsDefinition();            
        }
    }

My question is: Is there any need/use of 'explicit' implementation of interface in this piece of code? Will it create any problem if i remove the method (explicit implementation of interface) that i have marked "redundant" above?

PS: I understand that explicit implementation of interface is very important, and it can be used when we need to give access to a method at interface level only, and to use two interface with same signature of method.

like image 873
Manish Basantani Avatar asked May 03 '10 06:05

Manish Basantani


People also ask

Why are go interfaces implicit?

A type implements an interface by implementing its methods. There is no explicit declaration of intent, no "implements" keyword. Implicit interfaces decouple the definition of an interface from its implementation, which could then appear in any package without prearrangement.

What is implicit in inheritance in C#?

Implicit Inheritance Due to inheritance, this also means that all objects ever instantiated in C# can all use methods defined in System. Object . Because all objects in C# must have a type, and all objects in C# inherit from System. Object , the class Vegetable will be able to use methods defined on System.

Can we write method implementation in interface C#?

With C# 8.0, you can now have default implementations of methods in an interface. Interface members can be private, protected, and static as well. Protected members of an interface cannot be accessed in the class that extends the interface. Rather, they can be accessed only in the derived interface.

Can we override interface methods in C#?

Interface methods do not have a body - the body is provided by the "implement" class. On implementation of an interface, you must override all of its methods.


2 Answers

Yup. Looks redundant.

Calling it via a Customization type of reference and an ICustomization type of reference results in the same behavior. If you wanted the below calls to behave differently, then explicitly implementing the interface would have made sense.

Customization oVar = new Customization();
oVar.GetColumnsDefinition(); // calls 1st method
ICustomization iVar = obj;
iVar.GetColumnsDefinition(); // calls 2nd method - explicit impl.

You should remove the explicit implementation. However if you remove the other implementation, you will constrain clients such that they can no longer call oVar.GetColumnsDefintion() - they would have to use an interface variable as shown above.

like image 50
Gishu Avatar answered Oct 20 '22 02:10

Gishu


For info, the main time you see that specific pattern is when (any one of):

  • the non-explicit method is virtual or abstract, for subclasses to override
  • the signature of the public method is not quite the same, for example the public API has a more specific return type (common for things like IEnumerable[<T>] or ICloneable).
  • we don't want it to be public, but we want it to be easily callable within the type (without needing a nop-cast)

In this case it does indeed look redundant.

like image 25
Marc Gravell Avatar answered Oct 20 '22 02:10

Marc Gravell