Labels

Monday, March 9, 2009

Indexer Myth

Hi,

 

This is about how you create ‘Indexer’ in a class.

Indexer implementation provide birth to ‘Parametrized’ and ‘Default’ property concept.

 

class ColorManager

    {

        Hashtable colorsTable = new Hashtable();

       

        public Color this[string name] // Indexer

        {

            get

            {

                return colorsTable [name] as Color;

            }

            set

            {

                colorsTable.Add(name, value);

            }

        }

    }

class Color

    {

        private int red;

        private int green;

        private int blue;

 

        // Constructor

        public Color(int red, int green, int blue)

        {

            this.red = red;

            this.green = green;

            this.blue = blue;

        }       

    }

 

 

static void Main()

{

    ColorManager colormanager = new ColorManager();

 

    colormanager["red"] = new Color(255, 0, 0);

    colormanager["green"] = new Color(0, 255, 0);

    colormanager["blue"] = new Color(0, 0, 255);

}

 

 

Regards,

Arun Manglick

No comments:

Post a Comment