• Have Any Question?
  • contact@eitworld.com

csharp indexer





C# Indexer

 

    • Indexer allows instance of a class or structure to be indexed as array.
    • The main use of indexers is to support the creation of specialized arrays that are subject to one or more constraints.
    • Indexer is very similar to Properties but properties are used for return or set the specific data member but an Indexer return or set the particular value from the object instances.
    • As Properties are accessed through simple name but Indexer is accessed through an index.
    • The basic syntax of Indexer is shown below:
element-type this[int index] {
// The get accessor.
get {
// return the value specified by index
}
}
// The set accessor.
set {
// set the value specified by index
}
}

element type: Here element type is element type of Indexer, Thus, each element accessed by the indexer will be of type element-type. It corresponds to the element type of an array.Parameter ‘index’:The parameter index receives the index of the element being accessed.get:It is a accessor which return the value specified by the index.Set:It is a accessor that set the value specified by the index.

 

Here is an example of Indexer which is sufficient to understand the concept of Indexer:

 

Example

 

using System;
namespace Indexerexample
{
class student_name
{
string[] values = new string[100];//backing store
public string this[int number]
{
get
{
//this is invoked when object of student_name class access with []
if(number>0&&number<values.Length)
{
return values[number];//if bound in the range
}
else
{
return “Error”;
}
}
set
{
//this is invoked when obejct of student_name class assigned with []
if(number>0&&number<values.Length)
{

values[number]=value;//assign the element store in internal array
}
}
}
static void Main()
{
//create new objects and assign values through Indexer
student_name name = new student_name();
name[1] = “Radha”;
name[2]=”Krishna”;
name[3]=”Priya”;
name[-1]=”Error”;
name[1000] = “Error”;
//read elements through the indexer
string name_value1 = name[1];
string name_value2 = name[2];
string name_value3 = name[3];
string name_value4 = name[70];
string name_value5 = name[-1];
//write the result
Console.WriteLine(name_value1);
Console.WriteLine(name_value2);
Console.WriteLine(name_value3);
Console.WriteLine(name_value4);//null value
Console.WriteLine(name_value5);
Console.ReadLine();
}
}
}

This program includes a student_name class with an indexer we define. The student_name class contains an indexer that has get and set method bodies. These accessors have logic that ensures the Array will not be accessed out-of-bounds. The output of the above program will be as follows:

 

Output

 

Radha
Krishna
Priya

Error

Precautions while using Indexers

 

  • An indexer does not define a storage location, so a value produced by an indexer cannot be passed as a ref or out parameter to a method.
  • An indexer cannot be declared static.


Real Time Web Analytics