Indexer:
An Indexer is a Special type of Property that allows a Class or Structure to be Accessed the Same way as Array. Its a new concept in C# to treat an Object as an Array. The Indexers are Usually known as smart arrays in C# Programming. Indexer is similar to a Property. As with Properties, we use get and set Accessor to access and manipulate the Private properties of class. same like the Property, we use these set and get accessor with Indexer to Obtain a value from the Object itself, rather to Obtain a specific data member in Properties.
Defining Indexer:
The simplest version of an indexer is the One-Dimensional type just like an array. A One-Dimensional Indexer accepts a single value between the Square brackets when used. The Syntax used to declare the indexer is similar to that used to define the Property however, instead of defining a Property name, the Accessors are Declared for this[] as follows.
Access_Modifier Return_Type this[ Parameter_Type index ]
{
get
{
//Return the Value specified by index:
}
set
{
//Set the value Specified by index:
}
}
Different Between Indexers and Properties |
|
Indexers | Property |
---|---|
We use Set and get Accessors to returns or sets a Particular value from the object Instance. | We use set and get accessors to return or set a Specific data members. |
To Define the Indexers we use this Keyword along with array access operator ([ ]) which refers to the object instance. | To define the Property, we use to define it with names. |
Indexers are accessed using Indexes. | Properties are accessed by their names. |
Indexer are instance members, so can't be static. | Properties can be static as well as instance members |
A get accessor of an indexer has the same formal parameter list as the indexer. | A get accessor of a property has no parameters. |
Let's have an Example to know, how we can implement an Indexer, and how to use it.
/*Example - Indexers - InfoBrother*/
using System;
namespace Indexers
{
class Program
{
static public int size = 10;
private string[] Names = new string[size];
public Program() //constructor:
{
for(int i=0; i<size; i++)
{
Names[i] = "Seat Available";
}
}
public string this[int index] //Indexers.
{
get //Get accessor:
{
string temp;
if(index >= 0 && index < size)
{
temp = Names[index];
}
else
{
temp = "";
}
return (temp);
}
set //set accessor:
{
if(index >= 0 && index < size)
{
Names[index] = value;
}
}
}
static void Main(string[] args) //main method:
{
Program data = new Program(); //Object
data[0] = "Sardar Omar";
data[1] = "Taimur Parvez";
data[2] = "Muhammad Ammar";
data[3] = "Ayesha Sarwar";
data[4] = "Sana javed";
data[5] = "Jessa Rose";
Console.WriteLine("INFOBROTHER TEAM MEMBERS: ");
for(int i=0; i < Program.size; i++)
{
Console.WriteLine(">> " + data[i]);
}
Console.ReadKey();
}
}
}
Indexers:
INFOBROTHER TEAM MEMBERS: >> Sardar Omar >> Taimur Parvez >> Muhammad Ammar >> Ayesha Sarwar >> Sana javed >> Jessa Rose >> Seat Available >> Seat Available >> Seat Available >> Seat Available
In Above Example, first of all we create an string Array Named Names. it is an private Array so external users can't see it. Then in Constructor we fills each element with the word Seat Available using for-loop. The next class member is the Indexer, which is identified by This keyword and Square brackets. and finally using set and get accessor, we set and get the value from the object instance.
“
The array operator [] is nothing but an indexer implemented in all the data type in C#. For example, string[] is an indexer in the String class.
Overloaded Indexers:
Indexers can be overloaded. Indexers can also be declared with multiple parameters and each parameter may be a different type. It is not necessary that the indexes have to be integers. C# allows indexes to be of other types, for example, a string.
Let's try to overload our above example:
/*Example - Indexers Overloading - InfoBrother*/
using System;
namespace Indexers
{
class Program
{
static public int size = 10;
private string[] Names = new string[size];
public Program() //constructor:
{
for(int i=0; i<size; i++)
{
Names[i] = "Seat Available";
}
}
public string this[int index] //Indexers.
{
get //Get accessor:
{
string temp;
if(index >= 0 && index < size)
{
temp = Names[index];
}
else
{
temp = "";
}
return (temp);
}
set //set accessor:
{
if(index >= 0 && index < size)
{
Names[index] = value;
}
}
}
public int this[string name] //Overloaded Indexer:
{
get
{
int index = 0;
while (index < size)
{
if (Names[index] == name)
{
return index;
}
index++;
}
return index;
}
}
static void Main(string[] args) //main method:
{
Program data = new Program(); //Object
data[0] = "Sardar Omar";
data[1] = "Taimur Parvez";
data[2] = "Muhammad Ammar";
data[3] = "Ayesha Sarwar";
data[4] = "Sana javed";
data[5] = "Jessa Rose";
Console.WriteLine("INFOBROTHER TEAM MEMBERS: ");
//Using 1st Indexer with int parameter:
for(int i=0; i < Program.size; i++)
{
Console.WriteLine(">> " + data[i]);
}
//using 2nd Indexer with string parameter:
Console.WriteLine("The Name Sana is Available at Index No: "
+ data["Sana javed"]);
Console.ReadKey();
}
}
}
Indexers Overloading:
INFOBROTHER TEAM MEMBERS: >> Sardar Omar >> Taimur Parvez >> Muhammad Ammar >> Ayesha Sarwar >> Sana javed >> Jessa Rose >> Seat Available >> Seat Available >> Seat Available >> Seat Available The Name Sana is Available at Index No: 4