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

Indexer with custom types in C Sharp (C#) - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an indexer in the class.

C Sharp (C#)
public string this[[1] index] { get { return "Value" + index; } }
Drag options to blanks, or click blank then click option'
Aint
Bstring
Cdouble
Dbool
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of int for the index parameter.
Forgetting to specify the parameter type.
2fill in blank
medium

Complete the code to define a custom type used as an indexer parameter.

C Sharp (C#)
public struct [1] { public int Id; }
Drag options to blanks, or click blank then click option'
AIndex
BKey
CItem
DValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class instead of a struct.
Using a generic or unrelated name.
3fill in blank
hard

Fix the error in the indexer declaration using the custom type.

C Sharp (C#)
public string this[[1] key] { get { return "Value" + key.Id; } }
Drag options to blanks, or click blank then click option'
AKey
Bint
Cstring
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using int or string instead of the custom type.
Using object which does not have the Id property.
4fill in blank
hard

Fill both blanks to complete the indexer with get and set accessors using the custom type.

C Sharp (C#)
private Dictionary<[1], string> data = new Dictionary<[2], string>();
public string this[Key key] {
    get => data[key];
    set => data[key] = value;
}
Drag options to blanks, or click blank then click option'
AKey
Bint
Cstring
Dobject
Attempts:
3 left
💡 Hint
Common Mistakes
Using different types for the dictionary keys.
Using primitive types instead of the custom type.
5fill in blank
hard

Fill all three blanks to complete the indexer that checks if the key exists before returning a value or default.

C Sharp (C#)
public string this[[1] key] {
    get {
        if (data.[2](key))
            return data[key];
        else
            return [3];
    }
}
Drag options to blanks, or click blank then click option'
AKey
BContainsKey
Cnull
DTryGetValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using TryGetValue incorrectly.
Returning an empty string instead of null.
Using wrong parameter type.