Wednesday, 7 March 2012

What is indexer in dotnet?


Yes,till now we know indexing an array.it can be integer array,string array,bool array…ect but now onwords we can indexing an object also with the help of Indexers.
Using Indexers objects to be indexed like an array.
Syntax:
<Modifier>  <returntype>  [argumentlist]
{
Get{       }Set{         }
}
Like Ex:
class StringIndexer
    {
        private string []  data = new string[5];
        public string this [int index]
        {
        get
        {
        return data[index];
        }
        set
        {
        data[index] = value;
        }
        }
        static void Main()
        {
            StringIndexer obj=new StringIndexer();           
            obj[0] = "Rajesh";
            obj[1] = "A3-126";
            obj[2] = "Snehadara";
            obj[3] = "Irla";
            obj[4] = "Mumbai";          
            Console.WriteLine("{0},{1},{2},{3},{4}",obj[0],obj[1],obj[2],obj[3],obj[4]);
            Console.ReadKey();  
        }
    }
Here return type is string so if we indexing the the object of stringindex class which will return the string type of value only.
If we use the object as return type of indexer which can return any type of data.So in IList interface indexer is defined as object.
Gets or sets the element at the specified index.
  object this[int index] { get; set; }
so all collections implements this are indexed. So all collections  Arraylist as well as List<T>  type are indexed through indexers.
List<T> implements the indexer so all objects of List can use this funcationality.
As per  the type(intger,string,double,student..ect)
Gets or sets the element at the specified index.
 T this[int index] { get; set; }
   static void Main()
        {
          
            int[] arr = new int[3];
            arr[0] = 200;
            arr[1]=150;
            Console.WriteLine(arr[0]);
            Console.WriteLine("Array implements IList  ");         

            List<int> intlist = new List<int>();
            intlist.Add(15);
            Console.WriteLine(intlist[0]);
            Console.WriteLine("List<T> implements IList<T>");


            ArrayList arrlist = new ArrayList();
            arrlist.Add("kasi");
            arrlist.Add(10);
            arrlist.Add(new Student(104, "kasi"));
            Console.WriteLine(arrlist[2]);
            Console.WriteLine("ArrayList implements IList(not IList<T>)");           

            Student stu = new Student(101, "naga");
            Student stu1 = new Student(102, "kasi");           
            List<Student> obj = new List<Student>();
            obj.Add(stu);
            obj.Add(stu1);
            Student sindex = obj[0];
            Console.WriteLine(sindex.Stname);
            Console.ReadKey();
       
        }
ArrayList implements IList only why because which can return any type of data.not only a specific type of data.which will return string,int, another object,dobule…ect
return type(ArrayList) is object why because it can return any type of data like int,string,double, student,employe,job…….ect.
So which using
  object this[int index] { get; set; } of IList implementation
coming to
List<T> implements IList<T> specific type of data.
List<int> ,List<string>,List<student>,List<double> ….ect.and inder accepts at least one parameter  in the syntax of [arugments list].

No comments:

Post a Comment