Monday, November 1, 2021

C# Indexer


C# Indexer

Indexer is a special type of property in C# using which the values of fields of the class can be retrieved or set them based on their index positions.

Usually there is a property corresponding to each field which helps to get or set that field but property is a name using which we get or set that field. On the contrary, indexer is such a device with the help of which class is converted as a virtual array and by giving index values to the class we can get or set any field of that class. For example, if there is a class Employee, then if indexer is defined on the class then Employee(0), Employee(1), Employee(2) etc. will indicate the first, second and third fields of Employee class.

Syntax of indexer

  this( int index)
{
    get{}
    set{}
}
In the above example, instead of index, another name like position, i etc. can also be written.

The modifier of the index can be public, protected, etc.

If all the fields of the class are of the same type, then in that case we give the type of the index to the type of the field. For example, suppose all the fields of a class are of type string, then in such a case we will give the type string of the indexer but if the fields of the class are of different types, then in such a case give the type object of the indexer.

We write the logic to get a field within the get block whereas write the logic to set a field within the set block inside the indexer.

Now let us understand about indexer with an example.

namespace IndexerExample
{
    class Employee
    {
        int EmpId;
        string EmpName;
        string Department;
        double salary;

        public Employee(int EmpId, string EmpName, string Department, double Salary)
        {
            this.EmpId = EmpId;
            this.EmpName = EmpName;
            this.Department = Department;
            this.Salary = Salary;
        }

        public object this [int index]
            {
            get
            {
                if (index == 0)
                    return EmpId;
                else if (index == 1)
                    return EmpName;
                else if (index == 2)
                    return department;
                else if (index == 3)
                    return salary;
                else
                    return null;
            }
            set
            {
                //indexer returns object by default
                //We typecast to appropriate type as per the type of field
                if (index == 0)
                    EmpId = (int)value;
                else if (index == 1)
                    EmpName = (string)value;
                else if (index == 2)
                    Department = (string)value;
                else if (index == 3)
                    Salary = (double)value;
            }
        }
    }
}

using System;

namespace IndexerExample
{
    class program
    {
        static void Main(string[] args)
        {
            Employee employee = new Employee(1,"Ajeet","Sales",20000);
            Console.WriteLine("GET the old values:");
            Console.WriteLine("ID: "+ employee[0]);
            Console.WriteLine("Name: " + employee[1]);
            Console.WriteLine("Department: " + employee[2]);
            Console.WriteLine("Salary: " + employee[3]);
            Console.WriteLine("SET the new values:");
            employee[0] = 2;
            employee[1] = "Rajan";
            employee[2] = "IT";
            employee[3] = 23560.50;
            Console.WriteLine("ID: " + employee[0]);
            Console.WriteLine("Name: " + employee[1]);
            Console.WriteLine("Department: " + employee[2]);
            Console.WriteLine("Salary: " + employee[3]);

            Console.ReadLine();
        }
    }
}

Edited on 5th July 2023

No comments:

Post a Comment

Hot Topics