Your own indexer

The following code uses the indexer to access each week day.


using System;
class Week
{
    string[] days = new string[] { "Monday", "Tuesday", "Wednesday", 
                    "Thursday", "Friday", "Saturday", "Sunday" };

    public string this[int i]
    {
        get
        {
            return days[i];
        }
        set
        {
            days[i] = value;
        }
    }
}


class Program
{
    static void Main(string[] args)
    {
        Week w = new Week();

        Console.WriteLine(w[2]);


    }
}

The output:


Wednesday

The indexer can have the following modifiers:

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.