C#.NET indexers — Which statements are correct about indexer definitions and interface indexers?

Difficulty: Medium

Correct Answer: 1) The signature is the number/types of parameters. 2) Indexers are like properties but accessors take parameters. 4) The indexer's type and parameter types must be at least as accessible as the indexer.

Explanation:


Introduction / Context:
Indexers are property-like members that use parameters to provide array-style access to objects. Understanding their signatures and how they are declared on interfaces is essential for robust API design.



Given Data / Assumptions:

  • We evaluate five statements about indexers and their accessors.
  • We consider both class/struct indexers and interface indexers.


Concept / Approach:
Indexer signature is defined by the number and types of parameters (and modifiers like ref/out, when applicable). Indexers resemble properties, but accessors take parameters. Interface members, including indexers, have no bodies, and their accessors cannot use access modifiers; they simply declare get and/or set.



Step-by-Step Solution:

Statement 1: True — signature is based on parameter list.Statement 2: True — they are property-like but parameterized.Statement 3: False — interface accessors cannot have modifiers on accessors themselves.Statement 4: True — accessibility must be at least as accessible as the indexer.Statement 5: False — interface members have no bodies.


Verification / Alternative check:
Define an interface with int this[int i] { get; set; }; note there are no bodies and no accessor-level modifiers.



Why Other Options Are Wrong:

  • Options C: Both statements selected are false.
  • Option A: Mixes one true with two false statements.
  • Option D: Omits statement 1; incomplete.


Common Pitfalls:
Trying to add public/private to interface accessors or writing method bodies inside interfaces (not allowed).



Final Answer:
1, 2, 4

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion