CSharp - What is the output: Indexers with explicit interface

Question

What is the output from the following code

using System;


interface IMyInterface
{
    int this[int index] { get; set; }
}
class MyClass : IMyInterface
{
    private int[] myIntegerArray = new int[4];
    //Explicit interface implementation
    int IMyInterface.this[int index]
    {
        get => myIntegerArray[index];
        set => myIntegerArray[index] = value;
    }
}
class Program
{
    static void Main(string[] args)
    {
        MyClass obMyClass = new MyClass();
        IMyInterface interOb = (IMyInterface)obMyClass;
        //Initializing 0th, 1st and 3rd element using indexers
        interOb[0] = 20;
        interOb[1] = 21;
        interOb[3] = 23;
        for (int i = 0; i < 4; i++)
        {
            Console.WriteLine("\t obMyClass[{0}]={1}", i, interOb[i]);
        }
        Console.Read();
    }
}


Click to view the answer

obMyClass[0]=0
obMyClass[1]=1
obMyClass[2]=0
obMyClass[3]=3

Note

The code above is an example where we are implementing an interface explicitly with indexers.

An explicit implementation of an indexer is nonpublic.

An interface indexer does not have a body.

An interface indexer does not have modifiers.

Since C# 7.0, we can write codes like this:

public int MyInt
{
             get => myInt;
             set => myInt = value;
}

An explicit implementation of an indexer is non-public and non-virtual.

Related Quiz