0
0
C Sharp (C#)programming~10 mins

Multi-parameter indexers in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multi-parameter indexers
Create object with multi-parameter indexer
Call indexer with 2 parameters
Indexer get or set method runs
Access or modify internal data
Return or store value
Use value in main code
This flow shows how a multi-parameter indexer is called with two parameters, accesses internal data, and returns or sets a value.
Execution Sample
C Sharp (C#)
class Matrix {
  private int[,] data = new int[2,2];
  public int this[int row, int col] {
    get => data[row, col];
    set => data[row, col] = value;
  }
}

var m = new Matrix();
m[0,1] = 5;
var x = m[0,1];
This code creates a 2x2 matrix with a multi-parameter indexer to get and set values by row and column.
Execution Table
StepActionParametersInternal Data AccessResult/Output
1Create Matrix object-data initialized as [[0,0],[0,0]]-
2Set value via indexerrow=0, col=1, value=5data[0,1] set to 5-
3Get value via indexerrow=0, col=1data[0,1] accessed, value=55
4Use value in main code--x = 5
5End--Program ends
💡 All steps complete; program ends after value retrieval.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
data[[0,0],[0,0]][[0,5],[0,0]][[0,5],[0,0]][[0,5],[0,0]]
xundefinedundefined55
Key Moments - 2 Insights
Why do we use two parameters inside the indexer brackets?
Because the indexer is defined to take two parameters (row and column), so both are needed to access the correct element in the 2D array, as shown in execution_table step 2 and 3.
What happens if we try to get a value before setting it?
The value returned would be the default for int, which is 0, because the array is initialized with zeros. This is implied in step 1 where data starts with zeros.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of data after step 2?
A[[0,0],[5,0]]
B[[5,0],[0,0]]
C[[0,5],[0,0]]
D[[0,0],[0,5]]
💡 Hint
Check the 'Internal Data Access' column in step 2 of execution_table.
At which step is the value 5 assigned to variable x?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the 'Result/Output' column for step 4 in execution_table.
If we changed m[0,1] = 5 to m[1,0] = 7, what would data look like after step 2?
A[[0,0],[7,0]]
B[[0,5],[0,0]]
C[[7,0],[0,0]]
D[[0,0],[0,7]]
💡 Hint
Remember data is a 2D array indexed by [row, col], see variable_tracker for data layout.
Concept Snapshot
Multi-parameter indexers let you use multiple values inside [] to get or set data.
Syntax: public type this[type1 i, type2 j] { get; set; }
Useful for 2D data like matrices.
Access like obj[i,j].
Behind the scenes, get or set runs with those parameters.
Full Transcript
This example shows how a C# class can use a multi-parameter indexer to access elements in a 2D array. The indexer takes two parameters, row and column, to get or set values inside the internal array. The execution table traces creating the object, setting a value at position [0,1], then getting that value back. The variable tracker shows how the internal data array changes after setting the value, and how the variable x receives the retrieved value. Key moments clarify why two parameters are needed and what happens if you get a value before setting it. The visual quiz tests understanding of data changes and variable assignments during execution. The concept snapshot summarizes the syntax and purpose of multi-parameter indexers in simple terms.