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

Indexer declaration 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 that returns an int.

C Sharp (C#)
public int this[[1] index] { get { return index; } }
Drag options to blanks, or click blank then click option'
Aint
Bstring
Cbool
Ddouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using a return type instead of parameter type inside the brackets.
Using a type that is not valid for an indexer parameter.
2fill in blank
medium

Complete the code to declare a read-only indexer that returns a string.

C Sharp (C#)
public string this[int index] { [1] { return "value"; } }
Drag options to blanks, or click blank then click option'
Aset
Bprivate
Cinit
Dget
Attempts:
3 left
💡 Hint
Common Mistakes
Using set instead of get for reading values.
Using invalid accessor keywords.
3fill in blank
hard

Fix the error in the indexer declaration by completing the code.

C Sharp (C#)
public int this[int index] { get { return [1]; } }
Drag options to blanks, or click blank then click option'
Athis
Bindex
Cvalue
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Returning value inside a get accessor.
Returning this which is invalid here.
4fill in blank
hard

Fill both blanks to declare an indexer with get and set accessors.

C Sharp (C#)
public string this[int index] { [1] { return data[index]; } [2] { data[index] = value; } }
Drag options to blanks, or click blank then click option'
Aget
Bset
Cinit
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping get and set keywords.
Using invalid accessor names.
5fill in blank
hard

Fill all three blanks to declare an indexer that returns the uppercase key if value is not empty.

C Sharp (C#)
public Dictionary<string, string> data = new Dictionary<string, string>();
public string this[[1] key] {
    get {
        return data.ContainsKey(key) && data[key] [2] "" ? key.[3]() : null;
    }
}
Drag options to blanks, or click blank then click option'
Astring
B!=
CToUpper
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parameter type like int.
Using == instead of != in the condition.
Using incorrect method name for uppercase conversion.