using System;

class PwrOfTwo
{

    /* Access a logical array that contains
       the powers of 2 from 0 to 15. */
    public int this[int index]
    {
        // Compute and return power of 2.
        get
        {
            if ((index >= 0) && (index < 16)) return pwr(index);
            else return -1;
        }

        // there is no set accessor
    }

    int pwr(int p)
    {
        int result = 1;
        for (int i = 0; i < p; i++)
            result *= 2;

        return result;
    }
}

class UsePwrOfTwo
{
    public static void Main()
    {
        PwrOfTwo pwr = new PwrOfTwo();

        Console.Write("First 8 powers of 2: ");
        for (int i = 0; i < 8; i++)
            Console.Write(pwr[i] + " ");
        Console.WriteLine();

        Console.Write("Here are some errors: ");
        Console.Write(pwr[-1] + " " + pwr[17]);

        Console.WriteLine();
        Console.ReadKey();
    }
}


Once theraot that is the person who also has great knowledge of .NET and
replies many threads on this community, has told me that whenever we make
instance of any class that start running immediately. here we are making
instance of  PwrOfTwo Is this making
    public int this[int index] run immediately, this is in line no. 8
If yes then at that time does the execution of main method stops until the
control is passed again back to it.
If it stops running then where is "int index" getting its value from.
and if I am wrong which line of code is passing its control to indexer that
is     public int this[int index]

Reply via email to