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

Indexer declaration in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Indexer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this indexer usage?

Consider the following C# class with an indexer. What will be printed when the program runs?

C Sharp (C#)
using System;

class Sample {
    private string[] data = {"zero", "one", "two"};
    public string this[int index] {
        get { return data[index]; }
        set { data[index] = value; }
    }
}

class Program {
    static void Main() {
        Sample s = new Sample();
        s[1] = "ONE";
        Console.WriteLine(s[1]);
    }
}
Atwo
Bone
Czero
DONE
Attempts:
2 left
💡 Hint

Think about what happens when you assign a new value to s[1].

Predict Output
intermediate
2:00remaining
What error does this indexer code produce?

What error will this C# code produce when compiled?

C Sharp (C#)
class Test {
    private int[] values = new int[3];
    public int this[int index] {
        get { return values[index]; }
        // Missing set accessor
    }
}

class Program {
    static void Main() {
        Test t = new Test();
        t[0] = 10;
    }
}
ACompile-time error: Property or indexer cannot be assigned to -- it is read only
BRuntime error: Index out of range
CNo error, outputs 10
DCompile-time error: Missing get accessor
Attempts:
2 left
💡 Hint

Check if the indexer allows setting a value.

🔧 Debug
advanced
2:00remaining
Why does this indexer code cause a runtime error?

Examine the code below. Why does it throw an exception at runtime?

C Sharp (C#)
class Container {
    private int[] arr = new int[2];
    public int this[int index] {
        get {
            if (index < 0 || index >= arr.Length) throw new System.IndexOutOfRangeException();
            return arr[index];
        }
        set {
            arr[index] = value;
        }
    }
}

class Program {
    static void Main() {
        Container c = new Container();
        c[2] = 5;
    }
}
ABecause the get accessor throws an exception for index 2
BBecause the set accessor does not check index bounds before assignment
CBecause the array size is zero
DBecause the indexer is missing a set accessor
Attempts:
2 left
💡 Hint

Look at how the set accessor handles the index.

📝 Syntax
advanced
2:00remaining
Which option correctly declares a string indexer?

Which of the following C# indexer declarations is syntactically correct for indexing by a string key?

Apublic string this(key string) { get { return dict[key]; } set { dict[key] = value; } }
Bpublic string this[string key] get { return dict[key]; } set { dict[key] = value; }
Cpublic string this[string key] { get { return dict[key]; } set { dict[key] = value; } }
Dpublic string this[string key] => dict[key];
Attempts:
2 left
💡 Hint

Remember the syntax for indexers uses square brackets and braces.

🚀 Application
expert
3:00remaining
What is the output of this complex indexer example?

Given the following C# code, what will be printed?

C Sharp (C#)
using System;
using System.Collections.Generic;

class MultiIndexer {
    private Dictionary<(int, int), string> data = new();

    public string this[int x, int y] {
        get => data.TryGetValue((x, y), out var val) ? val : "none";
        set => data[(x, y)] = value;
    }
}

class Program {
    static void Main() {
        MultiIndexer mi = new MultiIndexer();
        mi[1, 2] = "A";
        mi[3, 4] = "B";
        Console.WriteLine(mi[1, 2]);
        Console.WriteLine(mi[2, 3]);
    }
}
A
A
none
B
none
none
C
A
B
D
B
none
Attempts:
2 left
💡 Hint

Check which keys are set and which are not.