C# Indexer: how to implement the concept of Indexer in c#


C# Indexer: in this article, I am going to show you how indexer works in c# programming with the help of step by step programming explanation.

C# Indexer

Indexers are a special form of properties that allow an index to be selected To access fields of a class.

Syntax of C# Indexer:


Instead of an identifier, C# Indexer uses the keyword this. This is followed in square brackets Type and identifier of the index variable. The get and set parts of the C# Indexer to generate a bidirectional mapping of the supported C#  index values on the elements.

Almost any data can be used as elements, from simple field values ​​to elements an internal array or a collection, up to calculated values ​​or database fields.

Example: how to implement the concept of C# indexer:

The class shown below defines an indexer that allows access to the elements in the internal values ​​collection as if they were array elements and the objects of the class were the Arrays.

First, make salesdata.txt file in the debug folder in my case the path is C:\Users\Shahzada Fawad\Documents\visual studio 2012\Projects\demo\demo\bin\Debug

Then save some data in this file in my case I save

C# indexer

Then paste the below code


C# indexer

 A C# Indexer allows considering an object as an array, each cell providing various object information. The operator [] for accessing an array can then be applied to the object. The index, that is, the value in square brackets, can be of any type and multiple C# Indexers can be specified (for example an index of type int and another of type string, as we will do in the following example). You can also find multiple values ​​(separated by commas) between the square brackets, which allows you to simulate access to arrays of several dimensions.

To implement one or more C# Indexers, you must write one or more functions called this and are written as in the following example (note the square brackets for the arguments to this). We create a Demo class and the square brackets give access to the different wives of XYZ.

The index can be a string. In this case, the C# Indexer returns a number wife or –1 in case of error. To simplify and focus on C# Indexers, no checking array overflow is not performed.

In the Demo class, we keep an array of wife names in private fields. It is created and initialized by the function Weddings. The index on the number is read/write while the index on the name is read-only. To simplify, no verification is carried out on the clues.


C# indexer

An obj object is an object, constructed like any other object, with new. But roger can be indexed, as if it were an array. When indexed to an integer, obj [i] returns (or modifies) the name of his wife. When it is indexed on a character string, roger [“Brigitte”] returns Brigitte’s serial number in obj list of wives (at different times).

C # allows us to go even further and easily scan obj indexers by writing with an example:

For this, our Demo class must implement the IEnumerator enumeration with its functions GetEnumerator, MoveNext, Reset, and the Current property:


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button