CSharp - What is the output: implement the interface explicitly

Question

What is the output from the following code

using System;

interface Interface1
{
    void ShowInterface1();
}
class MyClass : Interface1
{
    void Interface1.ShowInterface1()
    {
        Console.WriteLine("ShowInterface1() is completed.");
    }
}
class Program
{
    static void Main(string[] args)
    {
        MyClass myClassOb = new MyClass();
        myClassOb.ShowInterface1();//Error
                                   //Interface1 ob6 = myClassOb;
                                   //ob6.ShowInterface1();

    }
}


Click to view the answer

There is a compilation error.

Note

We have implemented the interface explicitly.

To access the explicit interface member, use the interface type.

To fix the error, use following lines of codes:

Interface1 ob6 = myClassOb;
ob6.ShowInterface1();

Alternatively, you can use the following line of code to get the same output:

((Interface1)myClassOb).ShowInterface1();

Related Quiz