Indexers allow instances of a class or struct to be indexed as arrays. The indexed value can be set or retrieved without explicitly specifying an instance type or a member. Indexers are like properties, except that their accessors take parameters.
The following example defines a generic class with some simple and established accessor methods for assigning and retrieving values. The program class creates an instance of this class to store strings.
using System;
public class SampleCollection
{
public string name { get; set; }
private string[] namecollection;
public SampleCollection(string name)
{
this.name = name;
this.namecollection = new string[10];
this.namecollection[0] = "Shahzad";
this.namecollection[1] = "Shahzad1";
this.namecollection[2] = "Shahzad2";
}
public void show()
{
for (int i = 0; i < this.namecollection.Length; i++)
{
if (this.namecollection[i] != null)
{
Console.WriteLine(this.namecollection[i]);
}
}
}
}
class Program
{
static void Main(string[] args)
{
SampleCollection sampleCollection = new SampleCollection("Shahzad");
sampleCollection.show();
}
}
Shahzad
Shahzad1
Shahzad2
In C# Indexer can also be generic.
using System;
class ExampleCollection<T>
{
private T[] array = new T[10];
public T this[int j]
{
get { return array[j]; }
set { array[j] = value; }
}
}
class Program
{
static void Main()
{
var MainCollection = new ExampleCollection<string>();
MainCollection[0] = "Shahzad Hussain";
MainCollection[1] = "Sabri";
for (int i = 0; i <= 9; i++)
{
if (MainCollection[i] != null)
{
Console.WriteLine(MainCollection[i]);
}
}
}
}
Shahzad Hussain
Sabri
Like functions, indexers can also be overloaded. In C # we can have multiple indexers in a single class. To override an index, you must declare it with multiple parameters, and each parameter must have a different data type. Indexers get overloaded bypassing 2 different kinds of parameters. This is very similar to method overload.
public class SampleCollection
{
public string name { get; set; }
private string[] namecollection;
public SampleCollection(string name)
{
this.name = name;
this.namecollection = new string[10];
this.namecollection[0] = "Shahzad";
this.namecollection[1] = "Shahzad1";
this.namecollection[2] = "Shahzad2";
}
public string this[int index]
{
get
{
if(index >= 0 && index< this.namecollection.Length)
{
return namecollection[index];
}else
{
return "-1";
}
}
set {
if (index >= 0 && index < this.namecollection.Length)
{
namecollection[index] = value;
}
}
}
public string this[int index,int index1]
{
get
{
return namecollection[index] + "\n" + namecollection[index1];
}
}
public void show()
{
for (int i = 0; i<this.namecollection.Length; i++)
{
if (this.namecollection[i] != null)
{
Console.WriteLine(this.namecollection[i]);
}
}
}
}
class Program
{
static void Main(string[] args)
{
SampleCollection sampleCollection = new SampleCollection("Shahzad");
//sampleCollection.show();
Console.WriteLine(sampleCollection[0]);
Console.WriteLine(sampleCollection[0, 1]);
}
}
Shahzad
Shahzad
Shahzad1