---
title: "C# Indexers – Access Objects Like Arrays"  
description: "C# allow objects to be indexed like arrays. This is useful when your object represents a collection of values and you want to access its elements as a"  
author: "Anubhav Sharma"  
published: 2025-04-22  
updated: 2025-04-22  
canonical: https://www.mindstick.com/blog/305504/c-sharp-indexers-access-objects-like-arrays  
category: "c#"  
tags: ["c#", "index"]  
reading_time: 3 minutes  

---

# C# Indexers – Access Objects Like Arrays

**Indexers** in C# allow objects to be **indexed like arrays**. This is useful when your object represents a [collection](https://www.mindstick.com/articles/1718/collections-in-java) of values and you want to access its elements using the array-like `object[index]` syntax.

## What Is an Indexer?

An indexer lets you use the **[array index](https://www.mindstick.com/interview/1100/why-array-index-starts-from-zero) syntax** (`[]`) on a class or struct to access [data stored](https://www.mindstick.com/forum/33481/in-objective-c-how-to-dump-data-stored-obj-c-object) inside it — just like an array or a list.

It’s defined using the `this` keyword followed by an index parameter.

## Syntax of an Indexer

```cs
class ClassName
{
    private string[] data = new string[10];

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

## Example: Creating and Using an Indexer

```cs
using System;

class StudentNames
{
    private string[] names = new string[5];

    // Define the indexer
    public string this[int index]
    {
        get { return names[index]; }
        set { names[index] = value; }
    }
}

class Program
{
    static void Main()
    {
        StudentNames students = new StudentNames();

        students[0] = "Alice";
        students[1] = "Bob";

        Console.WriteLine(students[0]); // Output: Alice
        Console.WriteLine(students[1]); // Output: Bob
    }
}
```

## Key Points

1. Indexers use the `this` keyword.
2. You can define `get` and/or `set` accessors.
3. You can overload indexers by changing the number or types of [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp).
4. Indexers can be **read-only**, **write-only**, or **read/write**.

## Indexers vs Properties

| Feature | Indexer | Property |
| --- | --- | --- |
| Access | Via index (e.g., obj[0]) | By name (e.g., obj.Name) |
| Syntax | `this[...]` | Property name |
| Use Case | Collection-like classes | Single values or settings |

## Example: String-based Indexer

You can also create indexers that accept strings or other data types.

```cs
class BookLibrary
{
    private Dictionary<string, string> books = new();

    public string this[string title]
    {
        get { return books.ContainsKey(title) ? books[title] : "Not found"; }
        set { books[title] = value; }
    }
}
```

## Usage

```cs
var library = new BookLibrary();
library["C#"] = "Learn C# in 21 Days";
Console.WriteLine(library["C#"]); // Output: Learn C# in 21 Days
```

## Access Modifiers in Indexers

You can set different [access modifiers](https://www.mindstick.com/articles/338838/explain-access-modifiers-in-java) for the `get` and `set` parts:

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

## Summary

| Concept | Description |
| --- | --- |
| What is it? | Allows class objects to be indexed like arrays |
| Defined by | Using `this[index]` with `get`/`set` |
| Return Type | Can be any type |
| Parameters | Can take any number and type |
| Use Case | Useful for custom [collections](https://www.mindstick.com/articles/12458/what-are-collections-in-c-sharp) and wrappers |

## Read More -

- [**Overview**](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/)
- [**Using Indexers**](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/using-indexers)
- [**Indexers in Interfaces**](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/indexers-in-interfaces)
- [**Comparison Between Properties and Indexers**](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/comparison-between-properties-and-indexers)

---

Original Source: https://www.mindstick.com/blog/305504/c-sharp-indexers-access-objects-like-arrays

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
