Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.
Indexers are a syntactic convenience that enable you to create a class , struct or interface that client applications can access just as an array. Indexers are most frequently implemented in types whose primary purpose is to encapsulate an internal collection or array. For example, suppose you have a class named TempRecord that represents the temperature in Farenheit as recorded at 10 different times during a 24 hour period. The class contains an array named "temps" of type float to represent the temperatures, and a datetime that represents the date the temperatures were recorded.
Indexers allow your class to be used just like an array.
On the inside of a class, you manage a collection of values any way you want.
These objects could be a finite set of class members, another array, or some complex data structure.
Regardless of the internal implementation of the class, its data can be obtained consistently through the use of indexers.
Indexer allows classes to be used in more intuitive manner. C# introduces a new concept known as Indexers which are used for treating an object as an array. The indexers are usually known as smart arrays in C#. They are not essential part of object-oriented programming.An indexer, also called an indexed property, is a class property that allows you to access a member variable of a class using the features of an array.Defining an indexer allows you to create classes that act like virtual arrays. Instances of that class can be accessed using the [] array access operator.