Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

C# Indexers: Syntax, Usage, and Tradeoffs

Learn how to declare and use C# indexers with the `this` keyword, including syntax, overloads, read-only access, and practical tradeoffs.

~3 min readC#
#C##Indexers#Object-Oriented Programming#Collection Types
Diagram showing a C# class with an indexer exposing array-like access to internal collection.

C# indexers let a class or struct expose array-like access to its internal data. Instead of forcing callers to work through a collection property, you can write instance[key] directly. An indexer is declared with the this keyword followed by a parameter list in square brackets. This article covers indexer syntax, parameters and overloads, read-only access, and when indexers are a good choice in production code.

What an Indexer Does

An indexer is a member that allows an instance of a class or struct to be indexed like an array. In the declaration, the this keyword does not work like the usual instance reference; it marks the member as an indexer. The compiler treats the member as a special property that takes one or more parameters.

Declaring an Indexer with the this Keyword

The basic declaration uses the this keyword followed by a parameter list in square brackets. The accessors get and set define how the indexed value is read and written.

csharp
public class StringCollection { private readonly List<string> _items = new(); public string this[int index] { get => _items[index]; set => _items[index] = value; } }

The get accessor returns the value at the given index, and the set accessor assigns to it. The value keyword represents the right-hand side of an assignment.

Indexer Parameters and Overloads

An indexer can accept more than one parameter and parameters of any type. This is useful for multidimensional or key-based access.

csharp
public class Matrix { private readonly double[,] _data; public double this[int row, int column] { get => _data[row, column]; set => _data[row, column] = value; } }

You can also overload indexers by parameter type, as long as the signatures differ.

csharp
public class Lookup { private readonly Dictionary<string, string> _byKey = new(); private readonly Dictionary<int, string> _byId = new(); public string this[string key] { get => _byKey[key]; set => _byKey[key] = value; } public string this[int id] { get => _byId[id]; set => _byId[id] = value; } }

Read-Only and Write-Only Indexers

If you omit the set accessor, the indexer is read-only. If you omit get, it is write-only. This is the same rule as properties. A read-only indexer is common when the underlying collection is immutable or when you want to expose only a projection.

csharp
public class ReadOnlyList { private readonly int[] _values = { 1, 2, 3 }; public int this[int index] => _values[index]; }

Common Use Cases for Indexers

Indexers are most useful when you want to provide a natural collection-like interface without exposing the internal storage. They are common in custom collection classes, adapter wrappers, and data access layers. For example, a repository might expose an indexer that looks up an entity by its primary key.

csharp
public class CustomerRepository { private readonly Dictionary<int, Customer> _customers = new(); public Customer this[int id] => _customers[id]; }

Performance and Maintainability Considerations

Indexers are not inherently slower than methods, but they can hide complexity. Every call goes through the accessor, so any validation or transformation you put inside the accessor runs on each access. If the accessor performs expensive work, such as a database query, be aware that the cost is incurred each time the indexer is used.

From a maintainability perspective, keep indexer logic simple. If the accessor becomes more than a few lines, consider whether a method with a descriptive name would be clearer. Indexers are best for direct, array-like access; they are not a substitute for well-named methods that express intent.

Indexers vs. Properties vs. Methods

Member typeSyntaxUse case
Propertypublic string Name { get; set; }Single value with a name
Indexerpublic string this[int i] { get; set; }Collection-like access by key or index
Methodpublic string GetByIndex(int i)Operation that may have side effects or multiple steps

The choice depends on how callers will naturally use the type. If you want obj[0] to read like an array, use an indexer. If you want obj.GetById(0) to read like a query, use a method.

Edge Cases and Limitations

Indexers cannot be static; they must be instance members. You cannot use ref or out parameters in an indexer, and the parameter list must contain at least one parameter.

Indexer parameter types are always checked at compile time against the declared signature. For example, this[int index] accepts only int arguments, while this[string key] accepts only string arguments. If you declare this[object key], callers may pass any type because the parameter type is object, not because a non-generic collection bypasses type checking.

When to Avoid Indexers

If the class has multiple ways to look up data, overloading indexers can become confusing. For example, having both this[int id] and this[string name] might make the intent unclear. In such cases, named methods like GetById and GetByName are often better. Indexers are also a poor fit when the accessor has significant side effects or requires complex validation that is better expressed as a method.

Try matching browser tools

All tools

Small utilities that fit C# work — they run entirely in this browser tab, so nothing you paste leaves your device.

All C# guides

More in C#

All C# guides

The guides filed next to this one in C#, in library order.