//An indexer allows an object to be indexed as an array. When we define an indexer for a class, this class behaves similar to a virtual array. We can then access the instance of this class using the array access operator ([ ]). Example is given below.
//We can have overloaded indexer in our application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Indexers_in_Csharp
{
class AnimalNames
{
int Length = 0;
string[] namelists = null;
AnimalNames(int index)
{
Length = index;
namelists = new string[Length];
}
public string this[int index]
{
get
{
string tmp;
if (index >= 0 && index <= Length - 1)
{
tmp = namelists[index];
}
else
{
tmp = "";
}
return (tmp);
}
set
{
if (index >= 0 && index <= Length - 1)
{
namelists[index] = value;
}
else {
throw new Exception("[" + index + "] invalid Length.", new IndexOutOfRangeException());
}
}
}
/// <summary>
/// Indexer overloading
/// </summary>
/// <param name="name">String name of animal. </param>
/// <returns>Index of name exist in AnimalNames else -1 (Not found)</returns>
public int this[string name]
{
get
{
int findindex = -1;
int index = 0;
while (index < Length)
{
if (namelists[index] == name)
{
findindex = index;
}
index++;
}
return findindex;
}
}
static void Main(string[] args)
{
AnimalNames names = new AnimalNames(5);
names[0] = "Cow";
names[1] = "Dog";
names[2] = "Donkey";
names[3] = "Elephant";
names[4] = "Tiger";
//names[7] = "Tiger";
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i]);
}
Console.WriteLine(names["Tiger"]);
Console.WriteLine(names["Ox"]);
Console.Read();
}
}
}
//We can have overloaded indexer in our application.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Indexers_in_Csharp
{
class AnimalNames
{
int Length = 0;
string[] namelists = null;
AnimalNames(int index)
{
Length = index;
namelists = new string[Length];
}
public string this[int index]
{
get
{
string tmp;
if (index >= 0 && index <= Length - 1)
{
tmp = namelists[index];
}
else
{
tmp = "";
}
return (tmp);
}
set
{
if (index >= 0 && index <= Length - 1)
{
namelists[index] = value;
}
else {
throw new Exception("[" + index + "] invalid Length.", new IndexOutOfRangeException());
}
}
}
/// <summary>
/// Indexer overloading
/// </summary>
/// <param name="name">String name of animal. </param>
/// <returns>Index of name exist in AnimalNames else -1 (Not found)</returns>
public int this[string name]
{
get
{
int findindex = -1;
int index = 0;
while (index < Length)
{
if (namelists[index] == name)
{
findindex = index;
}
index++;
}
return findindex;
}
}
static void Main(string[] args)
{
AnimalNames names = new AnimalNames(5);
names[0] = "Cow";
names[1] = "Dog";
names[2] = "Donkey";
names[3] = "Elephant";
names[4] = "Tiger";
//names[7] = "Tiger";
for (int i = 0; i < names.Length; i++)
{
Console.WriteLine(names[i]);
}
Console.WriteLine(names["Tiger"]);
Console.WriteLine(names["Ox"]);
Console.Read();
}
}
}