Last Updated : 11 Jul, 2025
In C#, indexers are special members that allow objects to be indexed similarly to arrays. Multi-dimensional indexers in C# are the same as multidimensional arrays. We can efficiently retrieve data with a multi-dimensional indexer by providing at least two parameters in its declaration. The first indexes the row dimension, the second indexes the column dimension, and so on.
Example 1: This example shows Multidimensional Indexers using get and set accessors.
C#
// C# program to illustrate the
// Multidimensional Indexers
using System;
class Geeks
{
// reference to underlying 2D array
int[,] data = new int[5, 5];
// declaring Multidimensional Indexer
public int this[int ind1, int ind2]
{
// get accessor
get
{
// it returns the values which
// read the indexes
return data[ind1, ind2];
}
// set accessor
set
{
// write the values in 'data'
// using value keyword
data[ind1, ind2] = value;
}
}
}
class main
{
public static void Main(String[] args)
{
// creating the instance of
// Class Geeks as "ind"
Geeks ind = new Geeks();
// assign the values accordingly to
// the indexes of the array
// 1st row
ind[0, 0] = 1;
ind[0, 1] = 2;
ind[0, 2] = 3;
// 2nd row
ind[1, 0] = 4;
ind[1, 1] = 5;
ind[1, 2] = 6;
// 3rd row
ind[2, 0] = 7;
ind[2, 1] = 8;
ind[2, 2] = 9;
Console.WriteLine("{0}\t{1}\t{2}\n{3}\t{4}\t{5}\n{6}\t{7}\t{8}",
ind[0, 0], ind[0, 1], ind[0, 2],
ind[1, 0], ind[1, 1], ind[1, 2],
ind[2, 0], ind[2, 1], ind[2, 2]);
}
}
Explanation: In the above example, we use a multidimensional indexer to access and modify elements in a 2D array. The get accessor retrieves values from the array, while the set accessor assigns values. The program fills the array and prints a 3x3 matrix.
Syntax of Multidimensional Indexerpublic DataType this[DataType row, DataType column]
{
get { return data[row, column]; }
set { data[row, column] = value; }
}
Example 2: Demonstration of Multidimensional Indexer using only get(Read-Only) accessor.
C#
// C# program to illustrate the Multidimensional
// Indexer using get accessor
using System;
class Geeks
{
// default constructor
public Geeks() { }
// Multidimensional Indexer
public int this[int i1, int i2]
{
// get accessor
get
{
// read only properties
return (i1 + i2);
}
// No set accessor used
}
public static void Main(String[] args)
{
// creating object of class
// "Geeks" as "ind"
Geeks ind = new Geeks();
// displaying the values
for (int i = 0; i <= 2; i++)
{
for (int j = 1; j <= 3; j++)
Console.Write(ind[i, j] + "\t");
Console.WriteLine();
}
}
}
Explanation: In the above example, we use a read-only multidimensional indexer where the get accessor returns the sum of the given row and column indices. The program iterates over the indices and prints the calculated values in a tabular format.
Example 3: Demonstration of Multidimensional Indexer using only set accessor to modify elements.
C#
// C# program to illustrate the
// Multidimensional Indexer using set accessor
using System;
class Geeks
{
// Private 2D array to store data
private int[,] data = new int[3, 3];
// Defining a multidimensional indexer
public int this[int row, int column]
{
get
{
// Return the value at the
// specified row and column
return data[row, column];
}
set
{
// Set the value at the
// specified row and column
data[row, column] = value;
}
}
static void Main(string[] args)
{
// Creating an instance
// of the Geeks class
Geeks ind = new Geeks();
// Assigning values using the indexer
ind[0, 0] = 1;
ind[1, 0] = 3;
// Retrieving a value
// using the indexer
int value = ind[1, 0];
Console.WriteLine(value);
}
}
Explanation: In the above example, we use a multidimensional indexer with only a set accessor to assign values to a 2D array. The program sets specific values and retrieves one element to demonstrate the assignment operation.
Example 4: Custom Logic in the Get Accessor
In this example, we demonstrate how to incorporate custom logic in the get accessor of a multi-dimensional indexer. Let’s consider a scenario where we want to return a specific element based on the provided indices.
C#
// Custom Logic in the Get Accessor
using System;
class Geeks
{
public int this[int row, int column]
{
get
{ // Custom logic
if (row == 0 && column == 0)
return 10;
else if (row == 1 && column == 1)
return 20;
else if (row == 2 && column == 2)
return 30;
else
return -1;
}
}
}
class main
{
static void Main(string[] args)
{
Geeks ind = new Geeks();
Console.WriteLine(ind[0, 0]);
Console.WriteLine(ind[1, 1]);
Console.WriteLine(ind[2, 2]);
}
}
Explanation: In the above example, we use a multidimensional indexer with a custom get accessor that returns predefined values (10, 20, 30) for specific indices. If the indices do not match predefined cases, it returns -1. This demonstrates how to apply conditional logic inside an indexer.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4