C# Indexers β Enable Array-Like Access to Custom Objects
Introduction β Why Use Indexers in C#?
Sometimes you want your object to behave like an array or collectionβallowing access to its internal elements using square brackets. Indexers in C# let you do exactly that. They allow objects to be indexed like arrays, improving usability, abstraction, and code readability.
In this guide, youβll learn:
- What indexers are and how they work
- How to define and use indexers in C#
- Real-world use cases and syntax patterns
- Best practices and tips for implementation
Core Concept β What Is an Indexer?
An indexer allows an object of a class or struct to be accessed using an index, similar to an array. It is defined using the this keyword and square brackets [].
Basic Syntax of an Indexer
class Sample
{
private string[] names = new string[3];
public string this[int index]
{
get => names[index];
set => names[index] = value;
}
}
this[int index] allows the object to be indexed like sample[0].
Code Example β Using an Indexer
Sample s = new Sample();
s[0] = "Alice";
s[1] = "Bob";
Console.WriteLine(s[0]); // Output: Alice
Explanation:
s[0] = "Alice";calls the indexer’ssetConsole.WriteLine(s[0]);calls the indexer’sget
Indexer with Multiple Parameters
class Matrix
{
private int[,] data = new int[3, 3];
public int this[int row, int col]
{
get => data[row, col];
set => data[row, col] = value;
}
}
Access like:
Matrix m = new Matrix();
m[0, 1] = 5;
Console.WriteLine(m[0, 1]); // Output: 5
Indexer vs Property
| Feature | Property | Indexer |
|---|---|---|
| Access Pattern | obj.PropertyName | obj[index] |
| Use Case | Named data member | Indexed collection-like access |
| Supports Multiple Params | Typically no | Yes |
Real-World Use Cases
- Custom collections (e.g.,
MyList<T>) - Representing matrix or grid data
- Wrappers over
Dictionary<TKey, TValue> - Table row/column mappings
- Form field access via keys
Best Practices & Tips
Tip: Validate the index to avoid IndexOutOfRangeException.
Pitfall: Donβt use indexers for actions with side effectsβit should only access or modify internal data.
Best Practice: Combine indexers with internal collections like arrays or lists.
Summary β Recap & Next Steps
Key Takeaways:
- Indexers allow custom objects to be indexed like arrays.
- Use
this[index]to define readable and writable access. - Support multiple parameters for 2D structures.
Real-world relevance: Widely used in custom data containers, matrix structures, and collection wrappers in C#.
FAQ Section
Can a class have more than one indexer?
No. A class can only have one indexer, but it can be overloaded with different parameter types.
Can indexers be overloaded?
Yes. You can define multiple indexers with different parameter types or counts.
Can I use string as an index?
Yes. Indexers support any type as an index:
public string this[string key] { get; set; }
Do indexers support access modifiers?
Yes. You can define separate access levels for get and set.
Are indexers the same as arrays?
No. Indexers just mimic array syntax but are custom-defined. Arrays are built-in types.
Share Now :
