Challenge - 5 Problems
Multi-parameter Indexer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of multi-parameter indexer access
What is the output of the following C# code using a multi-parameter indexer?
C Sharp (C#)
class Matrix { private int[,] data = new int[2, 2] { {1, 2}, {3, 4} }; public int this[int row, int col] { get { return data[row, col]; } set { data[row, col] = value; } } } var m = new Matrix(); Console.WriteLine(m[1, 0]);
Attempts:
2 left
💡 Hint
Remember that the first index is the row and the second is the column.
✗ Incorrect
The matrix is initialized as {{1, 2}, {3, 4}}. Accessing m[1, 0] means row 1, column 0, which is 3.
🧠 Conceptual
intermediate2:00remaining
Understanding multi-parameter indexer syntax
Which of the following correctly declares a multi-parameter indexer in C# that takes two integers and returns a string?
Attempts:
2 left
💡 Hint
Indexers use square brackets with comma-separated parameters.
✗ Incorrect
Option A uses the correct syntax: this[int x, int y]. Option A uses invalid syntax with multiple brackets. Option A uses semicolon instead of comma. Option A uses parentheses which is invalid for indexers.
🔧 Debug
advanced2:00remaining
Identify the error in multi-parameter indexer implementation
What error will the following code produce?
C Sharp (C#)
class Grid { private int[,] values = new int[3,3]; public int this[int x, int y] { get { return values[x]; } set { values[x, y] = value; } } }
Attempts:
2 left
💡 Hint
Check the getter's return statement and the type of values.
✗ Incorrect
The getter tries to return values[x], but values is a 2D array and values[x] returns an int[], which cannot be converted to int. This causes a compile-time error.
❓ Predict Output
advanced2:00remaining
Output of multi-parameter indexer with set and get
What will be printed after running this code?
C Sharp (C#)
class Table { private string[,] data = new string[2, 2]; public string this[int r, int c] { get => data[r, c]; set => data[r, c] = value.ToUpper(); } } var t = new Table(); t[0, 1] = "hello"; Console.WriteLine(t[0, 1]);
Attempts:
2 left
💡 Hint
Look at how the setter modifies the value before storing.
✗ Incorrect
The setter converts the string to uppercase before storing it. So when we get t[0,1], it returns "HELLO".
🧠 Conceptual
expert2:00remaining
Behavior of multi-parameter indexers with different parameter types
Given the following indexer declaration, which statement is true about its usage?
public int this[string key, int index] { get { return index + key.Length; } }
Attempts:
2 left
💡 Hint
Check the parameter types and how the indexer is accessed.
✗ Incorrect
The indexer takes a string and an int. Accessing obj["abc", 2] passes "abc" and 2. The getter returns 2 + 3 (length of "abc") = 5.