Conversation

Your input fuels progress! Share your tips or experiences on prioritizing mental wellness at work. Let's inspire change together!

Join the discussion and share your insights now!

Comments 0

Sharpen your coding skills—try JavaScript challenges on TOOLX now!

advertisement

Indexer in C# Programming

indexer in c#


Indexers

In a C# program, indexers allow instances of a class or struct to be indexed like arrays. Indexers are syntactically similar to properties, but unlike properties, the accessors of indexers accept one or more parameters.


1. Purpose of Indexers

Consider a high school teacher who wants to go through the records of a particular student to check the student's progress. If the teacher calls the appropriate methods every time to set and get a particular record, the task becomes a little tedious. On the other hand, if the teacher creates an indexer for student ID, it makes the task of accessing the record much easier. This is because indexers use the index position of the student ID to locate the student record.


2. Definition of Indexers

Indexers are data members that allow you to access data within objects in a way that is similar to accessing arrays. Indexers provide faster access to the data within an object as they help in indexing the data. In arrays, you use the index position of an object to access its value. Similarly, an indexer allows you to use the index of an object to access the values within the object.

The implementation of indexers is similar to properties, except that the declaration of an indexer can contain parameters. In C#, indexers are also known as smart arrays.


3. Declaration of Indexers

Indexers allow you to index a class, struct, or interface. An indexer can be defined by specifying the following:

  • An access modifier decides the scope of the indexer.
  • The return type of the indexer, which specifies the type of value an indexer, will return.
  • The this keyword refers to the current instance of the current class.
  • The bracket notation (0), consists of the data type and identifier of the index.
  • The open and close curly braces, contain the declaration of the set and get accessors.


4. Parameters

Indexers must have at least one parameter. The parameter denotes the index position, using which the stored value at that position is set or accessed. This is similar to setting or accessing a value in a single-dimensional array. However, indexers can also have multiple parameters. Such indexers can be accessed like a multi-dimensional array.

When accessing arrays, you must mention the object name followed by the array name. Then, the value can be accessed by specifying the index position. However, indexers can be accessed directly by specifying the index number along with the instance of the class.


5. Implementing Inheritance

Indexers can be inherited like other members of the class. This means that base class indexers can be inherited by the derived class.


6. Implementing Polymorphism

Indexers can implement polymorphism by overriding the base class indexers or by overloading indexers. By implementing polymorphism, a programmer allows the derived class indexers to override the base class indexers. In addition, a particular class can include more than one indexer having different signatures. This feature of polymorphism is called overloading. Thus, polymorphism allows the indexer to function with different data types of C# and generate customized output.


7. Multiple Parameters in Indexers

Indexers must be declared with at least one parameter within the square bracket notation (). However, Indexers can include multiple parameters. An indexer with multiple parameters can be accessed like a multi-dimensional array. A parameterized indexer can be used to hold a set of related values. For example, it can store and change values in multi-dimensional arrays.


8. Indexers in Interfaces

Indexers can also be declared in interfaces. However, the accessors of indexers declared in interfaces differ from the indexers declared within a class. The set and get accessors declared within an interface do not use access modifiers and do not contain a body. An indexer declared in the interface must be implemented in the class implementing the interface. This enforces reusability and provides the flexibility to customize indexers.


9. Difference between Properties and Indexers

Indexers are syntactically similar to properties. However, there are certain differences between properties and indexers.

Properties:

  • Properties are assigned unique names in their declaration.
  • Properties are invoked using the specified name.
  • Properties can be declared as static.
  • Properties are always declared without parameters.
  • Properties cannot be overloaded.
  • Overridden properties are accessed using the syntax base.prop, where is the name of the property.

Indexer:

  • Indexers cannot be assigned a name and use this keyword in their declaration.
  • Indexers are invoked through an index of the created instance.
  • Indexers can never be declared as static.
  • Indexers are declared with at least one parameter.
  • Indexers can be overloaded.
  • Overridden indexers are accessed using the syntax base [indExp], where indExp is the list of parameters separated by commas.  


C# Indexer in c# c# indexer interface definition of indexers in c# purpose of indexers in c# declaration of indexers in c# parameters in c# definition of parameter in c# implementing inheritance in c# implementing polymorphism in c# multiple parameters in

advertisement