Friday 1 December 2017

C# Program to Implement Use of Indexers


Code:

class values
{
    private int[] val = new int[10] { 10,20,30,40,50,60,70,80,90,100 }; 
    public int Length
    {
        get { return val.Length; }
    } 
    public int this[int index]
    {
        get
        {
            return val[index];
        }

        set
        {
            val[index] = value;
        }
    }
}
class MainClass
{
    static void Main()
    {
        values newval = new values();
        newval[3] = 58;
        newval[5] = 60;
        for (int i = 0; i < 10; i++)
        {
            System.Console.WriteLine("Element #{0} = {1}", i, newval[i]);
        }
        System.Console.WriteLine("Press any key to exit.");
        System.Console.ReadKey();

    }
}



Output:

Element #0 : 10
Element #1 : 20
Element #2 : 30
Element #3 : 58
Element #4 : 50
Element #5 : 60
Element #6 : 70
Element #7 : 80
Element #8 : 90
Element #9 : 100
Press any key to exit


More C# Programs:



















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...