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

Indexer with custom types in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Indexer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of indexer accessing custom type property

What is the output of this C# code?

C Sharp (C#)
public class Person {
    public string Name { get; set; }
    public Person(string name) { Name = name; }
}

public class Group {
    private Person[] people = { new Person("Alice"), new Person("Bob"), new Person("Charlie") };
    public Person this[int index] {
        get { return people[index]; }
        set { people[index] = value; }
    }
}

var group = new Group();
Console.WriteLine(group[1].Name);
AIndexOutOfRangeException
BCharlie
CBob
DAlice
Attempts:
2 left
💡 Hint

Remember that arrays are zero-based indexed.

Predict Output
intermediate
2:00remaining
Indexer set accessor effect on custom type array

What will be printed after running this code?

C Sharp (C#)
public class Item {
    public int Value { get; set; }
    public Item(int value) { Value = value; }
}

public class Container {
    private Item[] items = { new Item(10), new Item(20), new Item(30) };
    public Item this[int index] {
        get => items[index];
        set => items[index] = value;
    }
}

var container = new Container();
container[1] = new Item(99);
Console.WriteLine(container[1].Value);
A99
B20
C10
DNullReferenceException
Attempts:
2 left
💡 Hint

Check what happens when you assign a new Item to the indexer.

🔧 Debug
advanced
2:00remaining
Identify the error in this indexer implementation

What error will this code produce when compiled?

C Sharp (C#)
public class Box {
    private int[] data = new int[3];
    public int this[int index] {
        get { return data[index]; }
        set { data[index] = value; }
    }
}
ASyntaxError: Cannot use parentheses to index an array
BIndexOutOfRangeException at runtime
CNo error, compiles and runs fine
DNullReferenceException at runtime
Attempts:
2 left
💡 Hint

Look carefully at how the array is accessed in the set accessor.

Predict Output
advanced
2:00remaining
Output of indexer with custom struct type

What is the output of this code?

C Sharp (C#)
public struct Point {
    public int X, Y;
    public Point(int x, int y) { X = x; Y = y; }
    public override string ToString() => $"({X},{Y})";
}

public class Polygon {
    private Point[] points = { new Point(1,2), new Point(3,4), new Point(5,6) };
    public Point this[int index] {
        get => points[index];
        set => points[index] = value;
    }
}

var poly = new Polygon();
poly[2] = new Point(7,8);
Console.WriteLine(poly[2]);
ACompilation error
B(3,4)
C(5,6)
D(7,8)
Attempts:
2 left
💡 Hint

Remember structs are value types and can be replaced via indexer.

🧠 Conceptual
expert
2:00remaining
Why use indexers with custom types?

Which of the following is the best reason to implement an indexer with a custom type in C#?

ATo force all elements to be immutable and prevent changes
BTo allow accessing elements of a collection-like class using array syntax for better readability and encapsulation
CTo automatically serialize the class to JSON without extra code
DTo replace the need for constructors in the class
Attempts:
2 left
💡 Hint

Think about what indexers provide in terms of syntax and usage.