What are Indexers in C#?
π‘ Concept: Indexers
Indexers in C# allow objects of a class or struct to be indexed just like arrays. They are defined using the this
keyword and enable intuitive access to internal collections or logic.
π Quick Intro
Instead of calling methods like GetValue(i)
, indexers allow you to use obj[i]
. This makes class instances feel like collections and improves readability in data structures or APIs.
π§ Analogy
Think of an indexer like a vending machine. You insert a slot number (index) and get a snack (value). Internally, logic determines what snack to returnβbut you only interact using a simple number.
π§ Technical Explanation
- πΉ Declared using the
this
keyword with a parameter list. - πΉ Can have both
get
andset
accessors. - πΉ Can be overloaded based on parameter signatures.
- πΉ Provide cleaner syntax for class consumers.
- πΉ Common in wrapper classes around collections or custom containers.
π― Use Cases
- β Wrapping collections with business rules.
- β Exposing custom internal structures as if they were arrays.
- β Implementing matrix-like structures with two-dimensional indexers.
- β Providing multiple views into internal data (e.g., string indexer, int indexer).
π» Code Example
public class Week
{
private string[] days = { ""Sun"", ""Mon"", ""Tue"", ""Wed"", ""Thu"", ""Fri"", ""Sat"" };
public string this[int index]
{
get => days[index];
set => days[index] = value;
}
}
// Usage
var week = new Week();
Console.WriteLine(week[1]); // Output: Mon

β Interview Q&A
Q1: What is an indexer?
A: A member that allows access to a class or struct instance using array-like syntax.
Q2: How do you define an indexer in C#?
A: Using the this
keyword followed by a parameter inside square brackets.
Q3: Can indexers be overloaded?
A: Yes, with different parameter types or counts.
Q4: Can indexers be static?
A: No, indexers must be instance members.
Q5: Are indexers required to have both get and set?
A: No, you can have read-only or write-only indexers.
Q6: Can you use multiple parameters in indexers?
A: Yes, multi-dimensional indexers are allowed.
Q7: Is it possible to throw exceptions in indexers?
A: Yes, for example, when an index is out of range.
Q8: Are indexers inherited?
A: Yes, like methods and properties.
Q9: What is the return type of an indexer?
A: Any valid typeβitβs defined like a property.
Q10: How are indexers different from arrays?
A: Arrays are data structures; indexers are access mechanisms within a class.
π MCQs
Q1. What keyword is used to define an indexer?
- base
- var
- this
- index
Q2. Can you use multiple parameters in an indexer?
- No
- Only with arrays
- Yes
- Only with structs
Q3. Are indexers required to be public?
- Yes
- No
- Only static ones
- Only virtual ones
Q4. Can you throw exceptions in an indexer?
- No
- Yes
- Only get
- Only set
Q5. What does an indexer return?
- Only string
- Only int
- Nothing
- Any defined type
Q6. Can indexers be abstract?
- No
- Yes
- Only in structs
- Only in arrays
Q7. Can you define a write-only indexer?
- No
- Yes
- Only in sealed class
- Only in base class
Q8. Which access modifier is valid for indexers?
- Only public
- Only private
- Any valid modifier
- None
Q9. Are indexers similar to which other member?
- Methods
- Fields
- Properties
- Events
Q10. Can indexers be virtual?
- No
- Yes
- Only in interfaces
- Only in records
π‘ Bonus Insight
Indexers enhance usability by making your custom objects feel like arrays. They help encapsulate logic and improve API design for collection wrappers, caches, or matrix-like classes.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!