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

Multi-parameter indexers in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Multi-parameter Indexer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]);
A2
B4
C1
D3
Attempts:
2 left
💡 Hint
Remember that the first index is the row and the second is the column.
🧠 Conceptual
intermediate
2: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?
Apublic string this[int x, int y] { get { return ""; } }
Bpublic string this[int x; int y] { get { return ""; } }
Cpublic string this[int x][int y] { get { return ""; } }
Dpublic string this(int x, int y) { get { return ""; } }
Attempts:
2 left
💡 Hint
Indexers use square brackets with comma-separated parameters.
🔧 Debug
advanced
2: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; }
    }
}
ANo error, code compiles and runs
BRuntime IndexOutOfRangeException
CCompile-time error: Cannot convert int[] to int
DCompile-time error: Missing set accessor
Attempts:
2 left
💡 Hint
Check the getter's return statement and the type of values.
Predict Output
advanced
2: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]);
Ahello
BHELLO
CHello
Dnull
Attempts:
2 left
💡 Hint
Look at how the setter modifies the value before storing.
🧠 Conceptual
expert
2: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; } }
AYou can access it like obj["abc", 2] and it returns 5
BYou must pass two strings as parameters
CIt only accepts integer parameters
DIt cannot be used because indexers require parameters of the same type
Attempts:
2 left
💡 Hint
Check the parameter types and how the indexer is accessed.