Indexer allows classes to be used in more intuitive manner. C# introduces a new concept known as Indexers which are used for treating an object as an array. The indexers are usually known as smart arrays in C#. They are not essential part of object-oriented programming.
An indexer, also called an indexed property, is a class property that allows you to access a member variable of a class using the features of an array.
Defining an indexer allows you to create classes that act like virtual arrays. Instances of that class can be accessed using the [] array access operator.
Creating an Indexer
An indexer, also called an indexed property, is a class property that allows you to access a member variable of a class using the features of an array.
Defining an indexer allows you to create classes that act like virtual arrays. Instances of that class can be accessed using the [] array access operator.
Creating an Indexer
- < modifier > <
- return type > this[argument list] {
- get {
- // your get block code
- }
- set {
- // your set block code
- }
- }
<modifier>
can be private, public, protected or internal.
<return type>
can be any valid C# types.
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.
- Use code snippet to insert indexer syntax automatically in the visual studio.
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 thiskeyword with square bracket and paramters.
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 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;
}
}
}
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
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.
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.
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".
No comments:
Post a Comment