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

Indexer with custom types in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Indexer with custom types
Create class with custom type
Define private storage (e.g., array)
Define indexer method: get/set
Use indexer to access elements
Get or set custom type elements
End
The flow shows creating a class with a private storage of custom types, defining an indexer to get or set elements by index, and then using that indexer to access or modify elements.
Execution Sample
C Sharp (C#)
class Box {
  public string Content;
}

class Container {
  private Box[] boxes = new Box[2];
  public Box this[int i] {
    get => boxes[i];
    set => boxes[i] = value;
  }
}

var c = new Container();
c[0] = new Box { Content = "Apple" };
var fruit = c[0].Content;
This code defines a Container class with an indexer to get/set Box objects by index, then stores a Box with content "Apple" at index 0 and retrieves its content.
Execution Table
StepActionIndexer CallIndexValue Set/GetResult/Output
1Create Container instanceN/AN/AN/AContainer object created with boxes array of size 2 (all null)
2Set c[0]set0Box { Content = "Apple" }boxes[0] now holds Box with Content "Apple"
3Get c[0]get0N/AReturns Box with Content "Apple"
4Access Content propertyN/AN/AN/A"Apple" string retrieved from Box
5EndN/AN/AN/AExecution complete
💡 Indexer calls complete; program ends after retrieving content.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
cnullContainer instance with boxes[null, null]Same instanceSame instance
boxesnull[Box{Content="Apple"}, null][Box{Content="Apple"}, null][Box{Content="Apple"}, null]
fruitundefinedundefined"Apple""Apple"
Key Moments - 2 Insights
Why do we use an indexer instead of a normal method to access boxes?
The indexer lets us use array-like syntax (c[0]) to get or set elements, making code cleaner and more intuitive, as shown in execution_table steps 2 and 3.
What happens if we try to get an index that was never set?
The indexer returns the default value in the array, which is null for reference types like Box. This is implied in step 1 where boxes start as null.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of boxes[0] after step 2?
Anull
BBox with Content "Apple"
CEmpty Box
DString "Apple"
💡 Hint
Check the 'Value Set/Get' and 'Result/Output' columns in step 2.
At which step is the indexer 'get' accessor called?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Indexer Call' column to find where 'get' is used.
If we set c[1] to a new Box with Content "Orange", what changes in variable_tracker after step 2?
Aboxes remains [Box{Content="Apple"}, null]
Bfruit changes to "Orange"
Cboxes becomes [Box{Content="Apple"}, Box{Content="Orange"}]
Dc becomes null
💡 Hint
Think about how setting an indexer updates the boxes array.
Concept Snapshot
Indexer with custom types:
- Define private storage (e.g., array of custom objects)
- Use 'public Type this[int index]' with get and set
- Access elements like array: obj[index]
- Enables clean syntax to get/set custom objects
- Useful for container-like classes
Full Transcript
This example shows how to create a class with an indexer that works with custom types. We start by defining a class Container that holds an array of Box objects. The indexer allows us to get or set Box objects by index using array-like syntax. When we create a Container instance, its boxes array starts with nulls. Setting c[0] to a new Box with Content "Apple" stores that Box at index 0. Getting c[0] returns that Box, and accessing its Content property gives us the string "Apple". The execution table traces these steps, showing how the indexer is called for get and set, and how variables change. Key points include understanding why indexers simplify access and what happens if an index is not set. The quiz questions reinforce these ideas by asking about variable states and indexer calls.