I'm learning C#, I have this code :
namespace foo
{
public class Personnes
{
string[] m_Noms;
int m_NbElt;
int m_Max;
public Personnes(int Max)
{
m_Max = Max;
m_NbElt = 0;
m_Noms = new string[Max];
}
public int this[string Nom]
{
get { return Array.IndexOf(m_Noms, Nom); }
}
public string this[int i]
{
get { return m_Noms[i]; }
set { m_Noms[i] = value;m_NbElt++; }
}
}
class Prog
{
static void Main(string[] args)
{
Personnes Tableau = new Personnes(4);
Tableau[0] = "Anna";
Tableau[1] = "Ingrid";
Tableau[2] = "Maria";
Tableau[3] = "Ulrika";
Console.WriteLine(Tableau[1]);
Console.WriteLine(Tableau["Maria"]);
Console.WriteLine(Tableau[10]);
Console.WriteLine(Tableau["Toto"]);
}
}
}
I've been told that Console.WriteLine(Tableau[10]);
should display null and the next line -1 but it doesn't, instead I have an error IndexOutOfRange, why ?