We are familiar with implementing interfaces in C #, But we generally use implicit implementations. How can we use explicit implementations and when can we use explicit implementations? See the following example:
In the preceding example, both interface A and interface B have a testmethod method, whose signatures are the same, but the returned results have different types. Testclass implements both interface A and interface B. If the above example uses implicit implementation, it is impossible to compile it. Error message: Type 'leleapplication4. testclass' already defines a member called 'testmethod' with the same parameter types. In this case, explicit interfaces are required. Which of the following is true?CodeThe implicit implementation of interface A, while explicit implementation of interface B.
Interface A { Int Testmethod ();} Interface B { String Testmethod ();} Class Testclass: a, B { Public Int Testmethod (){ Return 10 ;} String B. testmethod (){ Return Testmethod (). tostring ();}}
Here is another example. If we define a class, it implements the ienumerable <t> interface. Let's take a look at the definition of the ienumerable <t> interface and find that it inherits the ienumerable interface.
Public InterfaceIenumerable <OutT>: Ienumerable {ienumerator<T>Getenumerator ();}
The ienumerable interface is defined:
Public InterfaceIenumerable {ienumerator getenumerator ();}
It can be seen that to implement the ienumerable <t> interface, two getenumerator methods are required, and one of them must be explicit:
ClassTestcontainer <t>: ienumerable <t>{PublicIenumerator <t>Getenumerator (){Throw NewNotimplementedexception ();} system. Collections. ienumerator system. Collections. ienumerable. getenumerator (){Throw NewNotimplementedexception ();}}
In summary, in general, we can only implement implicit interfaces, but when two interfaces contain methods with the same signature, we must implement them explicitly.