Summary: in this tutorial, you’ll learn how to use the C# indexer to make an object indexed like an array.
Introduction to the C# indexer
An index allows an object of a class to be indexed like an array. To define an indexer for a class, you use the following syntax:
class MyClass
{
public type this[int index]
{
get { // return a value }
set { // assing a value }
}
}Code language: C# (cs)The syntax of an indexer is like a property except that its accessor takes a parameter:
- First, define the
typeof the return value. - Second, define the indexer using the use
thiskeyword. - Third, define a parameter of the indexer. The parameter doesn’t have to be an integer. The parameter’s type depends on how you design the look-up mechanism. Also, an indexer can have more than one parameter.
- Finally, define the
getandsetaccessors. Thegetaccessor returns a value while thesetaccessor assigns a value. If the indexer doesn’t have thesetaccessor, it’s a readonly indexer.
Note that indexers can be overloaded. In other words, a class can have multiple indexers.
If you omit the set accessor, the indexer becomes read-only. In this case, you can use expression-bodied syntax to shorten its definition:
class MyClass
{
public type this[int index] => returnValue;
}Code language: C# (cs)Note that you don’t need to use the get keyword.
Starting with C# 7, you can use expression-bodied syntax for both get and set accessors. However, you need to use the get and set keywords in this case:
class MyClass
{
public type this[int i]
{
get => member[i];
set => member[i] = value;
}
}Code language: C# (cs)C# indexer examples
Let’s take some examples of using indexers.
1) C# readonly indexer example
Suppose you have a sentence like the following:
"C# is awesome"Code language: C# (cs)And you want to access each word of the sentence using an index. For example:
sentence[0]Code language: C# (cs)It should return the first word (C#), not the first character (C):
"C#"Code language: C# (cs)To do that, you can define a new class and use an indexer.
First, create a new file called Sentence.cs.
Second, define the Sentence class in the Sentence.cs file:
// Sentence.cs
class Sentence
{
private string[] words;
public Sentence(string s)
{
words = s.Split(' ');
}
}Code language: C# (cs)The Sentence() constructor splits an input string by the space into words and assigns the result to the words field.
Third, define an indexer in the Sentence class:
// Sentence.cs
class Sentence
{
private string[] words;
public Sentence(string s)
{
words = s.Split(' ');
}
public string this[int index]
{
get
{
return words[index];
}
}
}Code language: C# (cs)In this example, the indexer returns the word in the sentence based on an index.
The following illustrates how to use the indexer of the sentence’s object:
// Program.cs
var sentence = new Sentence("C# is awesome");
Console.WriteLine(sentence[0]);
Console.WriteLine(sentence[1]);
Console.WriteLine(sentence[2]);Code language: C# (cs)Output:
C#
is
awesomeCode language: C# (cs)Because the indexer in the Sentence class is read-only, you can shorten it using the expression-bodied syntax like this:
// Sentence.cs
class Sentence
{
private string[] words;
public Sentence(string s)
{
words = s.Split(' ');
}
public string this[int index] => words[index];
}Code language: C# (cs)2) C# indexer example with set and get accessors
The following example defines the class Matrix that uses a multidimensional array as a field and an indexer to access its elements:
// Matrix.cs
class Matrix
{
private double[,] data;
public Matrix(int row, int column)
{
data = new double[row, column];
}
public double this[int row, int column]
{
get { return data[row, column]; }
set { data[row, column] = value; }
}
}Code language: JavaScript (javascript)Now, you can use the Matrix’s object like an array like this:
// Program.cs
var matrix = new Matrix(3,3);
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3; column++)
{
matrix[row, column] = row + column;
}
}
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3; column++)
{
Console.Write($"{matrix[row, column]} ");
}
Console.WriteLine();
}
Code language: JavaScript (javascript)Output:
0 1 2
1 2 3
2 3 4Note that you can use the expression-bodied syntax for the get and set accessors of the indexer.
Summary
- Use C# indexer to enable an object to be indexed like an array.