C# - Indexer
An Indexer is a special type of property that allows a class or structure to be accessed the same way as array for its internal collection. It is same as property except that it defined with this keyword with square bracket and parameters.
Syntax:
Public <return type> this[<parameter type> index] { Get{ // return the value from the specified index } Set{ // set values at the specified index } }
The following example shows how to use indexer in the custom class.
Example: Indexer
class StringDataStore
{
private string[] strArr = new string[10]; // internal data storage
public string this[int index]
{
get
{
if (index < 0 && index >= strArr.Length)
throw new IndexOutOfRangeException("Cannot store more than 10 objects");
return strArr[index];
}
set
{
if (index < 0 || index >= strArr.Length)
throw new IndexOutOfRangeException("Cannot store more than 10 objects");
strArr[index] = value;
}
}
}
class Program
{
static void Main(string[] args)
{
StringDataStore strStore = new StringDataStore();
strStore[0] = "One";
strStore[1] = "Two";
strStore[2] = "Three";
strStore[3] = "Four";
for(int i = 0; i < 10 ; i++)
Console.WriteLine(strStore[i]);
}
}
Output:
OneTwo
Three
Four
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.
In the above example, StringDataStore class implements an indexer for its internal string array. So now, object of StringDataStore can be used like an array to add or retrive string data. We have used string array in the above example, you can also you any collection type as per your requirement.
Override Indexer
You can override an indexer by having different index types. The following example shows how an indexer can be of int type as well as string type.
Example: Indexer
class StringDataStore
{
private string[] strArr = new string[10]; // internal data storage
public StringDataStore()
{
}
public string this[int index]
{
get
{
if (index < 0 && index >= strArr.Length)
throw new IndexOutOfRangeException("Cannot store more than 10 objects");
return strArr[index];
}
set
{
if (index < 0 && index >= strArr.Length)
throw new IndexOutOfRangeException("Cannot store more than 10 objects");
strArr[index] = value;
}
}
public string this[string name]
{
get
{
foreach (string str in strArr){
if(str.ToLower() == name.ToLower())
return str;
}
return null;
}
}
}
class Program
{
static void Main(string[] args)
{
StringDataStore strStore = new StringDataStore();
strStore[0] = "One";
strStore[1] = "Two";
strStore[2] = "Three";
strStore[3] = "Four";
Console.WriteLine(strStore["one"]);
Console.WriteLine(strStore["two"]);
Console.WriteLine(strStore["Three"]);
Console.WriteLine(strStore["FOUR"]);
}
}
Output:
OneTwo
Three
Four
Insert Indexer Code snippet in VisualStudio
Vsual studio provides shortcut way to insert a code snippet for an indexer so that you don't have to write entire syntax manually. To insert a snippet for an indexer in Visual Studio, write idexer and press tab or do right click (or Ctrl + K,S) -> select "Insert Snippet.." -> select "Visual C#.." -> select "indexer".
Points to Remember:
- An indexer is same as property except that it defined with this keyword with square bracket that takes paramter.
- Indexer can be override by having different types of parameters.
- Ref and out parameter with the indexer is not supported.
- Indexer can be included as an interface member.
No comments:
Post a Comment