Tip of the Week: C# Indexers Made Easy
Hi, I will start a series of posts highlighting features of the .NET Framework (including C#, F#, etc.) to hopefully show you something that may have been overlooked in your programming journey. For this week’s tip, we’re going to talk about indexers in C#.
Imagine trying to find a book in a library without an index. It can be frustrating, right? You’d have to go from shelf to shelf, searching for the right book until you finally find it. Well, indexers in C# work like library indexes. They make finding information much easier and faster!
Indexers in C# allow you to access elements in a class, just like an array. But the cool thing is that you can define your own way of accessing those elements, based on your specific needs.
For example:
public class Library
{
private string[] books = new string[10];
public string this[int index]
{
get
{
return books[index];
}
set
{
books[index] = value;
}
}
}
In this example, we have a class called Library, which has an array of books. We define an indexer using the this
keyword, which takes an integer index and returns or sets the value of the corresponding element in the array.
So if we create an instance of the Library class, we can use the indexer to access its books:
Library myLibrary = new Library();
myLibrary[0] = "The Hitchhiker's Guide to the Galaxy";
myLibrary[1] = "1984";
string book = myLibrary[0]; // book = "The Hitchhiker's Guide to the Galaxy"
See how easy it is to access the elements of the Library class? It’s like a custom-made array, tailored to your needs!
But wait, there’s more! You can also define multiple indexers in a class, each with a different signature. For example:
public class Matrix
{
private int[,] values = new int[3, 3];
public int this[int row, int column]
{
get
{
return values[row, column];
}
set
{
values[row, column] = value;
}
}
public int this[string coordinate]
{
get
{
int row = (int)char.ToUpper(coordinate[0]) - 65;
int column = int.Parse(coordinate[1].ToString()) - 1;
return values[row, column];
}
set
{
int row = (int)char.ToUpper(coordinate[0]) - 65;
int column = int.Parse(coordinate[1].ToString()) - 1;
values[row, column] = value;
}
}
}
In this example, we have a class called Matrix, which has a two-dimensional array of integers. We define two indexers, one that takes two integer parameters and one that takes a string parameter. The first indexer returns or sets the value of the corresponding element in the two-dimensional array, while the second indexer converts a string coordinate (such as “A2” or “C1”) to the corresponding row and column and returns or sets the value of the corresponding element in the two-dimensional array.
For example:
Matrix myMatrix = new Matrix();
// Set values using the integer indexer
myMatrix[0, 0] = 1;
myMatrix[0, 1] = 2;
myMatrix[0, 2] = 3;
myMatrix[1, 0] = 4;
myMatrix[1, 1] = 5;
myMatrix[1, 2] = 6;
myMatrix[2, 0] = 7;
myMatrix[2, 1] = 8;
myMatrix[2, 2] = 9;
// Get values using the integer indexer
int value = myMatrix[1, 2]; // value = 6
// Set values using the string indexer
myMatrix["A2"] = 10;
myMatrix["C1"] = 11;
// Get values using the string indexer
int value2 = myMatrix["A2"]; // value2 = 10
int value3 = myMatrix["C1"]; // value3 = 11
So, indexers in C# are a powerful feature that allows you to define your own way of accessing elements in a class, making your code more flexible and easier to use. I hope you found this tutorial helpful!