Explicit and Implicit Interface Implementation in C#

Consider the code below.

Public interface  IstringInterface

{               String str {get;}

}

Public class StringClass: IstringInterface

{               //Implicit implementation

Public string str

{               Return “Implicit interface Implementation” ;

}

//explicit implemenatation

Public string IstringInterface.str

{               Return “Explicit interface Implementation” ;

}

}

Main()

{ StringClass obj = new StringClass();

//Implicit

Obj.str                    // work fine   Calling way 1

(IstringInterface (Obj)).str // works fine  calling way 2

//Explicit

Obj.str// not works  Calling way 1

(IstringInterface (Obj)).str // // works fine  calling way 2

}

For the implicit implementation Calling way 1 and Calling way 2 works fine. But Here, from the yellow line, if we use the explicit implementation then we will not be able to call the method directly, and only the calling way 2 will work.

Ebook Download
View all
Learn
View all