I've created a class "Person" and I want to do a while loop to let the user add objects until they want to stop, but it doesn't work.
I think the problem is I don't know how to create new objects from a list, but I'm unsure.
Here's my code:
static void Afficher(List <Personne> maliste)
{
foreach (var per in maliste)
{
per.ToString();
}
}
static void Ajouter(List<Personne> maliste)
{
string s;
bool stop = false;
int i = 0;
while(!stop)
{
Console.WriteLine("Entrez les informations ou entrez pour terminez!!");
Console.WriteLine("Entrez le nom de la personne numero "+ (i+1));
s = Console.ReadLine();
if (s == "") break;
maliste[i] = new Personne();
maliste[i].nom = s;
Console.WriteLine("Entrez le prenom de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
maliste[i].prenom = s;
Console.WriteLine("Entrez l'age de la personne numero " + (i + 1));
s = Console.ReadLine();
if (s == "") break;
maliste[i].age = int.Parse(s);
i++;
}
}
Error happens on maliste[i] = new Personne();
line:
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.