Mastering C# Indexers: The Secret Tool Every Developer Should Have!

The Hidden Gem You Need to Know!

DotNet Full Stack Dev
5 min read2 hours ago

If you’re familiar with arrays and collections in C#, you know how useful it is to access items by their position. But what if you could give that same experience to your classes? Enter indexers — a unique feature in C# that lets you create courses where individual elements can be accessed like an array. Indexers can make your classes more intuitive, more readable, and in some cases, incredibly powerful.

This guide will take you from zero to hero with indexers, explaining what they are and how to use them and sharing real-world examples along the way.

📌Explore more at DotNet Full Stack Dev
🌟 Clapping would be appreciated! 🚀

What Are Indexers?

In simple terms, an indexer in C# allows you to use your class like an array. You can access elements of the class using square brackets [], just as you would with an array. But instead of creating traditional properties or methods, you define an indexer, which gives you a shortcut to manage your data.

Here’s an analogy: think of indexers as “virtual shelves” in your custom data structures. With indexers, you can make your custom classes behave more like a list, dictionary, or other collections by providing direct, indexed access.

Defining Your First Indexer

Let’s break down the basics by creating a simple class with an indexer. Here’s a basic syntax:

public class MyClass
{
private int[] numbers = new int[10];

// Indexer declaration
public int this[int index]
{
get { return numbers[index]; }
set { numbers[index] = value; }
}
}

Explanation:

  • this[int index] is the indexer syntax. this refers to the current instance of the class, and [int index] indicates that this indexer takes an int parameter.
  • Inside the get accessor, we retrieve the element at the specified index.
  • Inside the set accessor, we assign a value to the element at that index.

Now, you can use MyClass as if it were an array:

MyClass myObj = new MyClass();
myObj[0] = 100; // Sets the first element to 100
int value = myObj[0]; // Gets the first element (100)

Real-World Example: Creating a Library Class with Indexers

Let’s go beyond the basics and see how indexers can improve readability in a more real-world context. Imagine you’re building a Library class to manage a collection of books. With an indexer, you can make retrieving and setting books a breeze.

public class Library
{
private Dictionary<int, string> books = new Dictionary<int, string>();

// Indexer to get or set books by ID
public string this[int id]
{
get
{
if (books.ContainsKey(id))
{
return books[id];
}
else
{
return "Book not found";
}
}
set
{
books[id] = value;
}
}
}

Now, interacting with the library feels natural and clean:

Library myLibrary = new Library();
myLibrary[1] = "The Great Gatsby"; // Adds book with ID 1
Console.WriteLine(myLibrary[1]); // Output: "The Great Gatsby"
Console.WriteLine(myLibrary[2]); // Output: "Book not found"

This structure provides a logical and intuitive way to handle your data without cumbersome GetBookByID or SetBookByID methods.

Multidimensional Indexers: Going Beyond the Basics

You can take indexers even further by using multiple parameters. This is especially useful for classes that need to represent multidimensional data, such as a 2D grid or matrix.

public class Grid
{
private int[,] gridData = new int[3, 3];

// Multidimensional indexer
public int this[int row, int col]
{
get { return gridData[row, col]; }
set { gridData[row, col] = value; }
}
}

Using this Grid class feels as natural as working with a 2D array:

Grid grid = new Grid();
grid[0, 0] = 5;
grid[1, 1] = 10;

Console.WriteLine(grid[0, 0]); // Output: 5
Console.WriteLine(grid[1, 1]); // Output: 10

With multidimensional indexers, you can manage data in rows and columns with ease, making them perfect for building board games, seat maps, and more.

Read-Only Indexers: When You Only Need get

Sometimes, you may want your indexer to be read-only, meaning that it only retrieves values but doesn’t allow setting them. You can do this by omitting the set accessor:

public class ReadOnlyCollection
{
private List<string> items = new List<string> { "Apple", "Banana", "Cherry" };

// Read-only indexer
public string this[int index]
{
get { return items[index]; }
}
}

Now, we can only access elements without modifying them:

ReadOnlyCollection readOnly = new ReadOnlyCollection();
Console.WriteLine(readOnly[1]); // Output: "Banana"

If we try readOnly[1] = "Orange";, it will give a compile-time error because the set accessor is not defined.

Adding Constraints and Validation

Just like properties, you can include validation in indexers to ensure that the values meet specific criteria. For example, we might want to restrict the Library class so that negative IDs can’t be used.

public class ValidatedLibrary
{
private Dictionary<int, string> books = new Dictionary<int, string>();

public string this[int id]
{
get
{
if (id < 0) throw new ArgumentException("ID must be non-negative");
return books.ContainsKey(id) ? books[id] : "Book not found";
}
set
{
if (id < 0) throw new ArgumentException("ID must be non-negative");
books[id] = value;
}
}
}

With this version of the Library, you’ll get an exception if you try using a negative ID, improving code quality and ensuring valid data.

When to Use Indexers (and When Not To)

Indexers are a great tool for making your classes act like collections, but they should be used thoughtfully. Here are some guidelines:

  • Use Indexers: When your class represents a collection of data, like a list, dictionary, or matrix. They work well in libraries, data grids, or other classes where indexed access makes sense.
  • Avoid Indexers: When indexed access isn’t intuitive for your class or might confuse other developers. For general properties and settings, use traditional properties.

Indexers can simplify code significantly in the right context. However, overusing them in scenarios where indexed access isn’t a natural fit can lead to harder-to-read code.

Wrapping It All Up

Indexers are like a secret weapon in C# that can turn your classes into highly readable, intuitive, and easy-to-use data structures. By using indexers, you make code access patterns more predictable and reduce clutter from accessor methods.

Whether you’re building a Library, a Grid, or any other custom data type, indexers let you handle data more elegantly. They may seem small at first, but they have a big impact on the cleanliness and clarity of your code.

Here’s what we covered:

  1. Basic syntax of indexers and why they’re useful.
  2. Building a real-world Library class with indexers.
  3. Creating multidimensional indexers for handling complex data.
  4. Using read-only indexers when only retrieval is needed.
  5. Adding validation to indexers for safe, consistent data access.

Conclusion: Start Adding Indexers to Your C# Toolkit!

Now that you know the power of indexers, think of where they could fit in your code. The next time you find yourself creating cumbersome accessor methods, consider whether an indexer could simplify your class structure. As you get more comfortable, you’ll discover that indexers can be one of the most elegant ways to make your code both powerful and expressive.

--

--

DotNet Full Stack Dev

Join me to master .NET Full Stack Development & boost your skills by 1% daily with insights, examples, and techniques! https://dotnet-fullstack-dev.blogspot.com