What is indexer?

What is indexer?

Indexer:-
                *
      Indexer Concept is object act as an array.
    *
      Indexer an object to be indexed in the same way as an array.
    *
      Indexer modifier can be private, public, protected or internal.
    *
      The return type can be any valid C# types.
    *
      Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error.

Signature:-
          his [Parameter]

{

    get

    {

        // Get codes goes here

    }

    set

    {

        // Set codes goes here

    }

} 
.................................................
Exapmle:-


 using System;

using System.Collections.Generic;

using System.Text;

 

namespace Indexers

{

    class PClass

    {

        private string[] range = new string[5];

        public string this[int indexrange]

        {

            get

            {

                return range[indexrange];

            }

            set

            {

                range[indexrange] = value;

            }

        }

    }

 

    /* The Above Class just act as array declaration using this pointer */

 

    class cclass

    {

        public static void Main()

        {

            PClass obj = new PClass();

 

               obj[0] = "ONE";

            obj[1] = "TWO";

            obj[2] = "THREE";

            obj[3] = "FOUR ";

            obj[4] = "FIVE";

            Console.WriteLine("WELCOME TO C# CORNER HOME PAGE\n");

            Console.WriteLine("\n");

 

            Console.WriteLine("{0}\n,{1}\n,{2}\n,{3}\n,{4}\n", obj[0], obj[1], obj[2], obj[3], obj[4]);

            Console.WriteLine("\n");

            Console.WriteLine("ALS.Senthur Ganesh Ram Kumar\n");

            Console.WriteLine("\n");

            Console.ReadLine();

        }

    }

} 

By:kamal
Date:2012-10-10 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Links

C interview questions and answers for experienced and fresher