Indexer in c#

By
Indexer is a new concept in c# language.This is very helpful when we want to use object as an array.If we want to use a class object like an array to set and get the values then we can create the Indexer within the class .
There are some feature of Indexer in c# language which is given below.
  • Indexer is written like property which will have get and set access-or .
  • Indexer access specifier can be Public ,private,internal,protected or protected Internal.
  • Return Type of Indexer is same as C# type.
  • Indexer takes at-least one parameter otherwise it gives compile time error. 
Now i am going implement Indexer concept in c# which is given below.Here i am using Console Application for Run the program.

Step-1 First open your visual studio->File->New->Project-Console Application->ok.->write the program which is given in Step-2.

Step-2  Indexer  C# program :-

using System;
namespace indexer1
{
    class Program
    {
        public class student
        {
            String[] name = new String[3];
            public String this[int i]
            {
                get
                {
                    if (i >= 0 && i < 3)
                    {
                        return name[i];
                    }
                    else
                    {
                        return "";
                    }
                }
                set
                {
                    if (i >= 0 && i < 3)
                    {
                        name[i] = value;
                    }
                }
            }

        }
        static void Main(string[] args)
        {
            student obj = new student();
            obj[0]="ram";
            obj[1]="shayam";
            obj[2]="india";
            obj[3]="Raj";
            Console.WriteLine(obj[0]);
            Console.WriteLine(obj[1]);
            Console.WriteLine(obj[2]);
            Console.WriteLine(obj[3]);//it will not printed because size of array is 3
            Console.ReadLine();
}
}
}
Step-3 Run the program(press F5).
See  output:-

Description:-  Please read it very carefully to know the Exact concept of Indexer .
  • Here I have First created a student class.
  • Then Created a array name of size 3.
  • Then created an Indexer name this[int i] with one parameter.
  • After that created get and set field to read and write the values.
  • After that created object of student class in main method and assigned the value in object indexer manually.
  • After that printed the corresponding object index value.
  • You can create only three object Indexer because array size is only 3 so that last value in object indexer will not print.
Note :- Here class act  as  a virtual array and we can access instance(object) of class  as an array.
For More:-

  1. Ado and Ado.Net
  2. Partial Class
  3. Directory and DirectoryInfo 
  4. Exception Handling
  5. Namespace
I Hope this helpful for you.
download whole application file
 Download

0 comments:

Post a Comment

Powered by Blogger.