Introduction
Indexers are instance members that allow class, struct and interfaces to be indexed just like an array, and the class behaves similar to a virtual array.
Indexer has no name but "get" and "set" accessor are like properties.
Suppose we have a private string array with the name Days, that holds the names of the weekdays and I want to access the names of the days using index. To achieve this, we use Indexer as shown below.
Indexers are instance members that allow class, struct and interfaces to be indexed just like an array, and the class behaves similar to a virtual array.
Indexer has no name but "get" and "set" accessor are like properties.
Suppose we have a private string array with the name Days, that holds the names of the weekdays and I want to access the names of the days using index. To achieve this, we use Indexer as shown below.
- public class Indexers {
- public string[] Days = {
- "Sunday",
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday"
- };
- public string this[int index] {
- get {
- return Days[index];
- }
- }
- }
- public class IndexerClient {
- public static void Main(string[] args) {
- Indexers IOb = new Indexers();
- Console.WriteLine(IOb[0]); // sunday
- Console.ReadKey();
- }
- }

Dnyaneshwar MulePosted Apr 26, 2017, 2:53 AM
Does this indexer have any advantage over enumeration ?