Introduction
Indexer allows accessing the members of a class as an array. In c# indexer is also known as smart array or virtual array. We can define an indexer using "this" keyword which indicates that we are creating the indexer for the current class. Once we have defined the indexer for a class, we can access the member of that class with the help of array access operator [ ]. So, we can say that indexer is similar to property but it allows us to access a member of the class using [ ] operator.
Why do we need to use indexer?
Let's consider a simple example.
Program 1
- namespace StudentClass
- {
- public class student
- {
- Int StudId;
- string StudName;
- public student(int StudId, string StudName)
- {
- this.StudId = StudId;
- this.StudName = StudName;
- }
- }
- }
In this example, we have created a simple student class with two members and a parametrized constructor. Once we have created a class and if it is public, then we can consume that class from any other class or project.
Program 2
- namespace consumestudent
- {
- public static void Main()
- {
- student stu = new student(1, ”Hitanshi”);
- stu[0]; //it is not accessible
- }
- }
In our second example, we have created a consumestudent class and we are creating one object of student class. By default, the scope for all members of the class is private, so we cannot access StudId and StudName outside the class. Now, if we want to access member outside a class, then there are three different mechanisms available.
- Declare member of a class as public
If we declare a member of class as public then anyone outside class can get or set the value of member. As a developer, we lose control over members.
Therefore, we should never declare a member as public. For example, if you declare a prize member as public, then anyone outside the class can set prize. - Use properties
Property allows restricted access. It allows readonly, writeonly, or read/write access. Property also allows us to set validation on the member. For example, we want to store the age only if it is greater than 18, then we can set a validation on properties before storing them into the database. - Use Indexer
Indexer is like property but it provides access to the members of class using an index value.
Syntax of Indexer
[<Modifiers>] <Type> this [<parameter list>]
Here, we use modifier as public, type as an object because it may return an integer, a string, or any other valid data type. We use "this" keyword to specify that we are declaring an indexer on the current class and the instance of the current class can have access on the members of a class. Parameter list can be any valid data type. Mostly, we use int and string as a parameter list. It means we can access the member of the class using int and string.
Where indexers are used?
We can use indexer in many ways,
Inside the class, we can declare an indexer as below.
Program 1
- namespace StudentClass
- {
- public class student
- {
- Int StudId;
- string StudName;
- public student(int StudId, string StudName)
- {
- this.StudId = StudId;
- this.StudName = StudName;
- }
- }
- public object this[int index]
- {
- get
- {
- If(index == 0)
- return StudId;
- elseif(index == 1)
- return StudName;
- }
- set
- {
- If(index == 0)
- StudId = (int) value;
- elseif(index == 1)
- StudName = (string) value;
- }
- public object this[string name]
- {
- get
- {
- If(name.ToUpper() == ”STUDID”)
- return StudId;
- elseif(name.ToUpper() = ”STUDNAME”)
- return StudName;
- }
- set
- {
- If(name.ToUpper() == ”STUDID”)
- StudId = (int) value;
- elseif(name.ToUpper() == ”STUDNAME”)
- StudName = (string) value;
- }
- }
- }
Here, the 'value' is an implicit variable that provide access to the value assigned by the user. The data type of the value will be the same as indexer so in our case, the value will be of object type. As value is object type, we need to perform the unboxing by converting the object into int or string.
- namespace consumestudent
- {
- public static void Main()
- {
- student stu = new student(11, ”Hitanshi”);
- stu[1] = ”HITANSHI”;
- Console.WriteLine(“Student Id” + stu[“Studid”]);
- Console.WriteLine(“Student name” + stu[“Studname”]);
- }
- }
11 HITANSHI
- namespace program {
- protected void page_Load(object sender, EventArgs a)
- {
- session[“data1”] = ”Show data1”;
- session[“data2”] = ”Show data2”;
- Response.WritLine(“Session1 stores” + session[0].ToString());
- Responser.WritLine(“Session2 stores” + session[“data2”].ToString());
- }
- }
- using(SqlConnection con = new SqlConnection(cs))
- {
- SqlCommand cmd = new SqlCommand(“select * from employee”, con);
- con.open();
- SqlDataReader rd = cmd.ExecuteReader();
- while (rd.Read())
- {
- Response.WritLine(“Id = ”+rd[0].ToString());
- Response.WritLine(“Name = ”+rd[“name”].ToString());
- }
- }
As illustrated in the example, we can get data from database with the help of string or integer indexer.

grandholyPosted Nov 15, 2018, 11:40 PM
Why doing this when we can just use: stu.StudId ?