Declaration of an indexer is to some extent similar to a property. The difference is that indexer accessors require an index.
Like a property, you use get and set accessors for defining an indexer. However, where properties return or set a specific data member, indexers return or set a particular value from the object instance.
class Clients {
private string[] names = new string[10];
public string this[int index] {
get {
return names[index];
}
set {
names[index] = value;
}
}
}
The indexer definition includes the this keyword and an index, which is used to get and set the appropriate value.
Now, when we declare an object of class Clients, we use an index to refer to specific objects like the elements of an array:
Clients c = new Clients();
c[0] = "Dave";
c[1] = "Bob";
Console.WriteLine(c[1]);
//Outputs "Bob"
namespace Solar_System
{
class SolarSystem
{
string[] planets = { "The Sun", "Mercury", "Venus", "Earth",
"Mars", "Jupiter", "Saturn","Uranus", "Neptune", "Pluto" };
private int GetPlanet(string testPlanet)
{
for (int j = 0; j < planets.Length; j++)
{
if (planets[j] == testPlanet)
{
return j;
}
}
throw new ArgumentOutOfRangeException
(testPlanet, "testPlanet must be correct name of the planet");
}
public int this[string planet]
{
get
{
return GetPlanet(planet);
}
}
}
class Program
{
static void Main(string[] args)
{
SolarSystem plan = new SolarSystem();
Console.WriteLine("Please enter name of planet to know its number from the set \"The Sun, Mercury, Venus, Earth,Mars, Jupiter, Saturn, Uranus, Neptune, Pluto\" ");
string st =Console.ReadLine();
st = st.UppercaseFirstLetter();
Console.WriteLine(plan[st]);
//Console.WriteLine(plan["Marsss"]);
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
}
And if the name of the planet is in the incorrect form, there is happening exception.
For example Marssss.