Indexes in C#

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpBasics
{
    //WE CANNOT HAVE STATIC INDEXERS IN C#
    class Indexes
    {
        List L = new List();

        public int this[int i]
        {
            get
            {
                return L[i];
            }
            set
            {
                L.Add(value);
            }
        }

        protected string this[string i]
        {
            get
            {
                return "hello";
            }
            set
            {
                //Do nothing;
            }
        }
    }

    class program
    {
        static void main(string[] args)
        {
            Console.WriteLine("****************Indexers****************");
            Indexes idxrs = new Indexes();
            idxrs[0] = 06;
            idxrs[1] = 19;
            idxrs[2] = 25;

            Console.WriteLine("Indexes get function : " + idxrs[0].ToString() +
                " " + idxrs[1].ToString() +
                " " + idxrs[2].ToString());
        }
    }
}

OUTPUT

****************Indexers****************
Indexes get function : 6 19 25
Tags: , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*