Open In App

C# | Multidimensional Indexers

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Indexers in C#

The multi-dimensional indexer is almost similar to multidimensional arrays. For the efficient content-based retrieval of data, multidimensional indexers are used. To create a multi-dimensional indexer you have to pass at least two parameters in the argument list of the indexer declaration. To access a single element of a multi-dimensional indexer, use integer subscripts. Each subscript indexes a dimension like the first indexes the row dimension, the second indexes the column dimension, and so on.

Example 1: Using get and set accessor

CSharp




// C# program to illustrate the 
// Multidimensional Indexers
using System;
   
   
class GFG {
       
    // reference to underlying 2D array
    int[, ] data = new int[5, 5];
       
       
    // declaring Multidimensional Indexer
    public int this[int index1, int index2]
    {
        // get accessor
        get
        {
               
             // it returns the values which
            // read the indexes
            return data[index1, index2];
               
        }
           
        // set accessor
        set
        {
               
            // write the values in 'data'
            // using value keyword
            data[index1, index2] = value;
               
        }
    }
}
   
// Driver Class
class Geeks {
       
       
    // Main Method
    public static void Main(String []args)
    {
           
        // creating the instance of 
        // Class GFG as "index"
        GFG index = new GFG();
           
   
        // assign the values accordingly to
        // the indexes of the array
        // 1st row
        index[0, 0] = 1;
        index[0, 1] = 2;
        index[0, 2] = 3;
           
        // 2nd row
        index[1, 0] = 4;
        index[1, 1] = 5;
        index[1, 2] = 6;
           
        // 3rd row
        index[2, 0] = 7;
        index[2, 1] = 8;
        index[2, 2] = 9;
       
        // displaying the values
        Console.WriteLine("{0}\t{1}\t{2}\n{3}\t{4}\t{5}\n{6}\t{7}\t{8}",
                                  index[0, 0], index[0, 1], index[0, 2],
                                  index[1, 0], index[1, 1], index[1, 2], 
                                  index[2, 0], index[2, 1], index[2, 2]);
       
    }
}


Output

1    2    3
4    5    6
7    8    9

Example 2: Without using the “set” accessor i.e. only using Read-Only Property

CSharp




// C# program to illustrate the Multidimensional 
// Indexer without using set accessor 
using System;
   
class GFG {
       
    // default constructor
    public GFG() {} 
   
    // Multidimensional Indexer
    public int this[int i1, int i2]
    {
           
        // get accessor
        get
        {
               
            // read only properties
            return (i1 + i2);
        }
           
        // No set accessor used
    }
   
// Main Method
public static void Main(String []args)
{
       
    // creating object of class
    // "GFG" as "index"
    GFG index = new GFG();
       
    // displaying the values
    for (int i = 0; i <= 2; i++) {
           
        for (int j = 1; j <= 3; j++)
        {
            Console.Write(index[i, j] + "\t");
        }
           
        Console.WriteLine();
    }
}
}


Output

1    2    3    
2    3    4    
3    4    5

Example 3: Adding a Set Accessor to Modify Elements

In this example, we extend the previous GFG class by adding a set accessor to modify the elements of the multi-dimensional indexer. This allows us to change the values at specific positions in the underlying data array.

C#




using System;
 
class Program
{
    private int[,] data = new int[3, 3];
 
    public int this[int row, int column]
    {
        get
        {
            return data[row, column];
        }
        set
        {
            data[row, column] = value;
        }
    }
 
    static void Main(string[] args)
    {
        Program index = new Program();
        index[0, 0] = 1;
        index[0, 1] = 2;
        index[1, 0] = 3;
 
        int value = index[1, 0];
        Console.WriteLine(value);
    }
}


Output

3

In this example, we define the GFG class with the data array as the underlying storage for the multi-dimensional indexer. The indexer has both get and set accessors. The get accessor retrieves the value from the data array, and the set accessor assigns a value to the specified position in the array.

We create an instance of the GFG class named index. We then assign values to the elements of the multi-dimensional indexer using the indexer syntax. Finally, we retrieve a specific value from the indexer and display it using Console.WriteLine().

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#




using System;
 
class GFG
{
     
 
    public int this[int row, int column]
    {
        get
        {
            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;  // Return a default value
        }
    }
}
 
// Usage:
class Program
{
    static void Main(string[] args)
    {
        GFG index = new GFG();
        Console.WriteLine(index[0, 0]);
        Console.WriteLine(index[1, 1]);
        Console.WriteLine(index[2, 2]);
    }
}


Output

10
20
30

In this example, we define the GFG class with the data array as the underlying storage for the multi-dimensional indexer. The get accessor incorporates custom logic to return specific values based on the provided indices.

We create an instance of the GFG class named index. We then access and display specific elements of the multi-dimensional indexer using the indexer syntax and Console.WriteLine().



Last Updated : 22 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads