---
title: "What are indexers in C#?"  
description: "What are indexers in C#?"  
author: "ICSM Computer"  
published: 2025-07-01  
updated: 2026-05-30  
canonical: https://www.mindstick.com/forum/161762/what-are-indexers-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 4 minutes  

---

# What are indexers in C#?

What are [indexers](https://www.mindstick.com/interview/406/what-are-the-indexers) in C#? [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) with example in details way.

## Replies

### Reply by Anubhav Sharma

In C#, **indexers** allow an object to be accessed like an array by using square brackets (`[]`). They provide a way to define how values are retrieved or assigned using an index.

## Why Use Indexers?

Without an indexer:

```cs
public class StudentCollection
{
    private string[] students = new string[5];

    public string GetStudent(int index)
    {
        return students[index];
    }

    public void SetStudent(int index, string value)
    {
        students[index] = value;
    }
}
```

Usage:

```cs
StudentCollection collection = new StudentCollection();

collection.SetStudent(0, "John");
Console.WriteLine(collection.GetStudent(0));
```

With an indexer, the syntax becomes much cleaner.

## Basic Indexer Syntax

```cs
public class StudentCollection
{
    private string[] students = new string[5];

    public string this[int index]
    {
        get
        {
            return students[index];
        }
        set
        {
            students[index] = value;
        }
    }
}
```

Usage:

```cs
StudentCollection collection = new StudentCollection();

collection[0] = "John";
collection[1] = "Alice";

Console.WriteLine(collection[0]);
Console.WriteLine(collection[1]);
```

Output:

```plaintext
John
Alice
```

## How It Works

The keyword `this` defines an indexer:

```cs
public string this[int index]
```

- `get` is executed when reading a value.
- `set` is executed when assigning a value.
- `value` represents the assigned value inside the setter.

Example:

```cs
collection[0] = "John";
```

Internally calls:

```cs
set
{
    students[0] = "John";
}
```

## Indexers with Multiple Parameters

Indexers can accept multiple parameters.

```cs
public class Matrix
{
    private int[,] data = new int[3, 3];

    public int this[int row, int column]
    {
        get
        {
            return data[row, column];
        }
        set
        {
            data[row, column] = value;
        }
    }
}
```

Usage:

```plaintext
Matrix matrix = new Matrix();

matrix[1, 2] = 100;

Console.WriteLine(matrix[1, 2]);
```

Output:

```plaintext
100
```

## String-Based Indexers

Indexers don't have to use integers.

```cs
public class EmployeeDirectory
{
    private Dictionary<string, string> employees =
        new Dictionary<string, string>();

    public string this[string employeeId]
    {
        get
        {
            return employees[employeeId];
        }
        set
        {
            employees[employeeId] = value;
        }
    }
}
```

Usage:

```cs
EmployeeDirectory directory = new EmployeeDirectory();

directory["E001"] = "John";
directory["E002"] = "Alice";

Console.WriteLine(directory["E001"]);
```

Output:

```plaintext
John
```

## Read-Only Indexer

If you only need retrieval:

```cs
public class Numbers
{
    private int[] data = { 10, 20, 30 };

    public int this[int index]
    {
        get
        {
            return data[index];
        }
    }
}
```

Usage:

```cs
Numbers numbers = new Numbers();

Console.WriteLine(numbers[1]);
```

Output:

```plaintext
20
```

## Write-Only Indexer

Although uncommon, you can create a write-only indexer:

```cs
public class Logger
{
    public string this[int index]
    {
        set
        {
            Console.WriteLine($"Value: {value}");
        }
    }
}
```

## Real-World Examples

Indexers are commonly used in:

- Custom collections
- Caching systems
- Data containers
- Grid or matrix implementations
- Wrappers around dictionaries
- Configuration managers

Examples in .NET:

`List<T>`

`Dictionary<TKey, TValue>`

`DataRow`

```cs
List<string> names = new List<string>();

names.Add("John");

Console.WriteLine(names[0]);
```

`List<T>` uses an indexer internally.

## Indexer vs Property

| Property | Indexer |
| --- | --- |
| Accessed by name | Accessed by index |
| `obj.Name` | `obj[0]` |
| Represents a single value | Represents a collection-like structure |
| Uses property name | Uses `this` keyword |

Example:

```cs
person.Name = "John";
```

vs

```cs
students[0] = "John";
```

## Interview Question

## Can a class have multiple indexers?

Yes, as long as their parameter signatures are different.

```cs
public class DataStore
{
    public string this[int index]
    {
        get { return "Integer Index"; }
    }

    public string this[string key]
    {
        get { return "String Key"; }
    }
}
```

Usage:

```cs
DataStore store = new DataStore();

Console.WriteLine(store[1]);
Console.WriteLine(store["A"]);
```

Output:

```plaintext
Integer Index
String Key
```

### Summary

An **indexer** is a special class member that lets objects be accessed using array-like syntax (`obj[index]`). It is declared using the `this` keyword and can have `get`, `set`, or both accessors. Indexers are particularly useful when building custom collections or data structures that should feel natural to use, similar to arrays, lists, and dictionaries.


---

Original Source: https://www.mindstick.com/forum/161762/what-are-indexers-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
