π 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’sset
Console.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 :