C
C#2y ago
DayKnight

❔ Foreach loop

I am trying to run through my list, but nothing is being printed out, what am I doing wrong?
List<Enrollment> tilmeldingslist = new List<Enrollment>();
Enrollment enroll1 = new Enrollment(Alex, Programmering);
Enrollment enroll2 = new Enrollment(Alex, Matematik);
Enrollment enroll3 = new Enrollment(Alex, Dansk);

foreach (var items in tilmeldingslist)
{
Console.WriteLine(items);
}
List<Enrollment> tilmeldingslist = new List<Enrollment>();
Enrollment enroll1 = new Enrollment(Alex, Programmering);
Enrollment enroll2 = new Enrollment(Alex, Matematik);
Enrollment enroll3 = new Enrollment(Alex, Dansk);

foreach (var items in tilmeldingslist)
{
Console.WriteLine(items);
}
8 Replies
.juansantiago
.juansantiago2y ago
Does enrollment class has a ToString() method?
DayKnight
DayKnight2y ago
No I am not sure why it isn't printing anything, because it should have values inside the list right?
WhiteBlackGoose
did you add elements to the list?
.juansantiago
.juansantiago2y ago
If you want to print the name and the enrollment class you should have a ToString() method on the class and format there your message
Aaron
Aaron2y ago
you didn't add anything to the list you just made the Enrollments without adding them
DayKnight
DayKnight2y ago
Oooh... A website suggests that I do this, but is this really what I am looking for?
authors.Add(new Author { Name = "Mahesh Chand", Book = "Graphics with GDI+", Price = 49.95 });
authors.Add(new Author { Name = "Mahesh Chand", Book = "Graphics with GDI+", Price = 49.95 });
Should I use .Add?
Aaron
Aaron2y ago
.Add does add things to the list, yes
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.