![]() |
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
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
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#
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#
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(). |
Reffered: https://www.geeksforgeeks.org
C# |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 8 |