Skip to content

6.3 For-loop through a List

  1. Remove the line ShowInformation(myCircles[1]); from your Main method.
  2. Add the following code to your Main method:

    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 = 2 means indices 0 and 1 are valid!
  3. Run your program. You will see the information about the two circles present.

  4. Remove the code above and add the following code:

    1
    2
    3
    4
    5
    foreach (Circle circle in myCircles)
    {
        ShowInformation(circle);
        Console.WriteLine();
    }
    

    circle is the variable name of type Circle that iterates through the myCircles list.

  5. Run your program. Your output is exactly the same as above.