Hi Megha,
Indexers in C# are a simple way to permit a class to be used like an array. The advantage of using indexers rather than exposing an array or generic list object is that internally, you can manage the values presented in any fashion you wish. This means that you can manage the array size, error handling and so forth with a lower likelihood of bugs. It allows you to better manage complex data structures, odd types of class members and so forth, hiding some complexity from other programmers who may not be familiar with a particular requirement. I've found indexers to be particularly useful in situations where data is being aggregated from several different sources but needs to be presented as a cohesive group. Below, I'll present a simple example of a C# indexer and you can take it from there.
First, lets begin with a simple string property that's accessed via an integer indexer. We'll assume that there is an array of strings that's loaded in the class constructor. Here's what this property looks like…
public string this[int index]
{
get
{
return DataItems[index];
}
set
{
DataItems[index] = value;
}
}
As you can see, we use the this keyword to define the indexer. Also notice that we use brackets [] rather than a parenthesis () in the definition. In this case, our indexer is an integer and we're expecting a string value to be set or returned by the property. But, the nice thing is that it can be overloaded. So, let's say that I wanted to be able to send in a string value or an integer value. Here's how it would be coded…
public string this[string key]
{
get
{
return DataItems[GetIndexForKey(key)];
}
set
{
DataItems[GetIndexForKey(key)] = value;
}
}
In this example, we're passing in a string value rather than an integer value to determine the return value. A private routine, GetIndexForKey, looks up the internal index value so that it can be set or returned properly in the underlying data. As you can see, overloading indexers can add a lot of flexibility to your code. The indexer could be called either way as seen in these two example lines of code…
IndexTest[1] = "This is a Test";
IndexTest["example"] = "This is a Test";
But, you can take it even further with by having an indexer with multiple parameters. This method is effective for returning individual items within class instances contained within the indexer class or to perform special processing on a the returned value. For example, if you were returning a date as a string value, you could pass in formatting information in addition to the index value, as in this example…
public string this[int index, string formatValue]
{
get
{
return DateItems[index].ToString(formatValue);
}
}
Please refer the below links
http://programmingincsharp.com/indexers-in-c/
http://www.c-sharpcorner.com/UploadFile/senthurganesh/109172007050741AM/1.aspx
http://www.c-sharpcorner.com/uploadfile/cd3aa3/indexers-in-C-Sharp/default.aspx
http://www.c-sharpcorner.com/uploadfile/puranindia/indexers-in-C-Sharp/default.aspx
http://www.c-sharpcorner.com/uploadfile/prvn_131971/indexers-in-C-Sharp/default.aspx
Thanks