Indexers in c# Dotnet
Loading
Indexers in c# Dotnet
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Chetan SanghaniRank #244 · 10.2K reputationPosted May 26, 2025, 2:47 AM EDT
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.public class SampleCollection { private T[] arr = new T[100]; public T this[int index] { get { return arr[index]; } set { arr[index] = value; } } }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.var collection = new SampleCollection(); collection[0] = "Hello"; Console.WriteLine(collection[0]); // Output: HelloWhen 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
public class DayCollection { private string[] days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; public string this[int index] { get { if (index = days.Length) throw new IndexOutOfRangeException("Invalid index"); return days[index]; } } } // Usage DayCollection week = new DayCollection(); Console.WriteLine(week[1]); // Output: MondayConclusion
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.
Kiran KumarRank #1,268 · 1.4K reputationPosted May 26, 2025, 4:58 AM EDT
Thanks I am implementing app apart from storing days any specific logic I can use this feature like storing connection string
Chetan SanghaniRank #244 · 10.2K reputationPosted Jul 9, 2025, 4:17 AM EDT
You can use indexers in a class that mimics configuration retrieval:
class ConnectionStringManager { private Dictionary _connections = new Dictionary(); public string this[string name] { get { return _connections.ContainsKey(name) ? _connections[name] : null; } set { _connections[name] = value; } } }Usage:
var manager = new ConnectionStringManager(); manager["Default"] = "Server=.;Database=AppDb;Trusted_Connection=True;"; Console.WriteLine(manager["Default"]);This use of an indexer gives you a clean syntax while still keying by name.
Natasha Sturrock57 reputationPosted Jul 10, 2025, 5:38 AM EDT
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:
public class SampleCollection { private string[] elements = new string[10]; public string this[int index] { get { return elements[index]; } set { elements[index] = value; } } }In this example,
SampleCollectionprovides array-like access to its internal string array via the indexer. Consumers can use it as:Key points about indexers:
Cynthia SathuragiriRank #937 · 2.2K reputationPosted Jul 10, 2025, 8:45 AM EDT
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