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

Indexer declaration in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Indexer declaration
Start
Declare class with indexer
Indexer get/set called with index
Access or modify internal data
Return or update value
End
This flow shows how a class with an indexer lets you use array-like access to get or set values inside the class.
Execution Sample
C Sharp (C#)
class Sample {
  private int[] data = {10, 20, 30};
  public int this[int i] {
    get => data[i];
    set => data[i] = value;
  }
}

var s = new Sample();
Console.WriteLine(s[1]);
s[1] = 50;
Console.WriteLine(s[1]);
This code defines a class with an indexer to get and set values in an internal array using square brackets.
Execution Table
StepActionIndexValue BeforeValue AfterOutput
1Create Sample instance----
2Read s[1]1202020
3Set s[1] = 5012050-
4Read s[1]1505050
5End----
💡 Finished accessing and modifying values using the indexer.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
data[0]1010101010
data[1]2020505050
data[2]3030303030
Key Moments - 2 Insights
Why can we use square brackets on the object s like s[1]?
Because the class Sample declares an indexer with 'this[int i]', it allows using square brackets to get or set values inside the object, as shown in steps 2 and 3 of the execution_table.
What happens if we try to access an index outside the array range?
An exception will occur because the indexer accesses the internal array directly without bounds checking. This is not shown in the current execution_table but is important to remember.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when reading s[1] at step 2?
A20
B50
C10
DError
💡 Hint
Check the Output column at step 2 in the execution_table.
At which step does the value at index 1 change in the data array?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the Value After column for data[1] in variable_tracker after each step.
If we remove the set accessor from the indexer, what would happen when executing step 3?
AIt would update the value successfully.
BIt would cause a compile-time error.
CIt would throw a runtime exception.
DIt would ignore the assignment silently.
💡 Hint
Remember that without a set accessor, the indexer is read-only and cannot assign values.
Concept Snapshot
Indexer declaration syntax:
public returnType this[type index] {
  get { ... }
  set { ... }
}

Allows objects to be accessed like arrays.
Get returns value at index.
Set updates value at index.
Useful for internal collections.
Full Transcript
This example shows how to declare and use an indexer in C#. The class Sample has a private array and an indexer that lets you get or set values using square brackets. When you create an instance and use s[1], it calls the get accessor returning the value at index 1. When you assign s[1] = 50, it calls the set accessor updating the value. The execution table traces these steps and shows how the internal array changes. Beginners often wonder why square brackets work on objects; it's because of the indexer declaration. Also, if you remove the set accessor, you cannot assign values, causing a compile error.