6.3 For-loop through a List
- Remove the line
ShowInformation(myCircles[1]);from yourMainmethod. -
Add the following code to your
Mainmethod:1 2 3 4 5
for (int i = 0; i < myCircles.Count; i++) { ShowInformation(myCircles[i]); Console.WriteLine(); }- Always start your for-loop with 0.
- The index must never equal
Count.Count = 2means indices 0 and 1 are valid!
-
Run your program. You will see the information about the two circles present.
-
Remove the code above and add the following code:
1 2 3 4 5
foreach (Circle circle in myCircles) { ShowInformation(circle); Console.WriteLine(); }circleis the variable name of typeCirclethat iterates through themyCircleslist. -
Run your program. Your output is exactly the same as above.