In this article, I am going to share with you about Indexer in C#
What is Indexer?
- Indexer is used for treating objects as an array.
- Indexer is a property that allows instances of class or struct to be indexed similar to an array.
- Indexer is also known as a smart array, or one can say indexer as Parameterized property.
- With Indexer, class or struct instances can be accessed using [] array access operator.
- Indexer makes the syntax-syntactic sugar. Means make code concise and easy to understand.
- Indexer uses 'get' and 'set' accessor similar to property. 'get' accessor returns a value. 'set' accessor set a value. The 'value' keyword used to define the value being assigned by the 'set' accessor.
Syntax of Indexer is,
- <access_modifier> <return_type> this [<Parameters_list>]
- {
- //Implementation of get and/or set accessor
- }
Here, access_modifier can be public, private, protected or internal return_type can be any valid return type(int, string, bool etc) other than void Parameters_list must contain one or more than one parameter.
Example
- public int this[int x]
- {
- get { return x; }
- set { value = x; }
- }
- Indexer name must be 'this' keyword.
- Indexer can be public, private, protected or internal.
- Indexer can be used when a class represents a list, collection or array of objects.
Code
Here, in the below problem, we have created an indexer in class 'Main'. With indexer, we are putting and retrieving values to an array of int type. Indexer uses get and set accessor to get and set value from/to the array. Indexer name should be 'this'. Inside class 'Program', we have created an object of class 'Main' and that object is now working similar to an array. The reason is one and only indexer. Indexer allows the object to be treated as an array as shown in the program. Index on which value is not assigned will be by default set to 0.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace Tutpoint
- {
- class Main
- {
- // Array of int type
- public int[] int_array = new int[10];
- // Indexer
- public int this[int x]
- {
- get
- {
- return int_array[x];
- }
- set
- {
- int_array[x] = value;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Main main = new Main();
- //Object treating as an array
- main[0] = 222;
- main[8] = 33;
- main[6] = 11;
- main[2] = 1;
- main[3] = 6;
- for (int i = 0; i < main.int_array.Length; i++)
- {
- Console.WriteLine(main.int_array[i]);
- }
- Console.ReadLine();
- }
- }
- }
Output
- 222
- 0
- 1
- 6
- 0
- 0
- 11
- 0
- 33
- 0
Note
Here for this program, we have to use index between 0 to 9 according to the size of an array else runtime 'IndexOutOfRangeException' will generate as "Index was outside the bounds of the array".
Some of the examples,
main[-3] = 6;
main[93] = 60;
- Indexer can be overloaded means that a class can contain more than one indexer but with different parameters.
In the below program, we have created 3 indexers with different parameters. An object created of the class 'Main' then be used with proper arguments and accordingly Indexer will be called. This is similar to method overloading.
Code

Join the conversation! Your thoughts help the community grow.