Difficulty: Easy
Correct Answer: 2) Student s = new Student(); s[3] = 34;
Explanation:
Introduction / Context:
Indexers in C#.NET let an object behave like an array using the syntax obj[index]. They are instance members; C# does not support static indexers. This problem asks which example statements correctly use such an indexed property of a Student class that maps to a 5-element integer array.
Given Data / Assumptions:
Concept / Approach:
Valid usage requires a Student instance (constructed with new) and then either assignment via the set accessor or reading via the get accessor. Static-like forms such as Student[3] are invalid. Also, constructs like Student.this are not C# syntax and should be rejected.
Step-by-Step Solution:
Verification / Alternative check:
Declare public int this[int i] { get; set; } inside Student, then compile and test statements 2 and 3 successfully.
Why Other Options Are Wrong:
Common Pitfalls:
Forgetting that indexers are instance-based; also mixing up property indexers with arrays declared at the type level.
Final Answer:
2, 3
Discussion & Comments