Indexers in c# Dotnet
Loading
Indexers in c# Dotnet
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Chetan SanghaniPosted May 26, 2025, 6:47 AM
Indexers in C# are special types of properties that allow objects of a class or struct to be indexed just like arrays. They enable instances of a class to be accessed using the array-access syntax (
object[index]), making your objects feel more intuitive and collection-like.Key Points:
Indexers are declared using the
thiskeyword followed by a parameter list.They can be overloaded (i.e., multiple indexers with different parameter types).
They can have any access modifier (
public,private, etc.).Indexers can be read-only, write-only, or read-write, just like properties.
They do not have a name – they are always named
this.When to Use Indexers
Use indexers when:
Your class represents a collection or logical grouping of data.
You want to simplify access to elements inside your class using intuitive syntax.
Real-world Example
Conclusion
Indexers are a powerful feature in C# that help make your classes more flexible and user-friendly, especially when dealing with internal collections or mappings. By providing array-like access to class data, they improve code readability and usability.
Cynthia SathuragiriPosted Jul 10, 2025, 12:45 PM
Storing Configuration Key-Value Pairs
public class AppSettings settings = new Dictionary();
{
private Dictionary
public string this[string key]
{
get => settings.ContainsKey(key) ? settings[key] : null;
set => settings[key] = value;
}
}
AppSettings config = new AppSettings();
config["ConnectionString"] = "Server=.;Database=DBname;Trusted_Connection=True;";
Console.WriteLine(config["ConnectionString"]);
This is a clean way to abstract key-value config data like connection strings, file paths, API keys
Natasha SturrockPosted Jul 10, 2025, 9:38 AM
In C#, indexers allow objects to be indexed like arrays, providing a way to access elements within a class or struct using the array access operator []. They are a powerful feature in custom .NET development that enables classes to behave like collections without exposing the internal data structures directly.
An indexer is defined with the this keyword followed by one or more parameters inside square brackets. It essentially defines a property that takes parameters, enabling syntax like myObject[index] to get or set values.
Here’s a simple example:
In this example,
SampleCollectionprovides array-like access to its internal string array via the indexer. Consumers can use it as:Key points about indexers:
Chetan SanghaniPosted Jul 9, 2025, 8:17 AM
You can use indexers in a class that mimics configuration retrieval:
Usage:
This use of an indexer gives you a clean syntax while still keying by name.
Kiran KumarPosted May 26, 2025, 8:58 AM
Thanks I am implementing app apart from storing days any specific logic I can use this feature like storing connection string