Skip to content

6.2 List

  1. If needed, start from the project up to chapter 5. This contains the classes as you left them at the end of chapter 5.
  2. Add the following code to your Main method:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    List<Circle> myCircles = new List<Circle>();
    myCircles.Add(new Circle(3, new Point(1, 4), "green"));
    myCircles.Add(new Circle());
    myCircles.Add(new Circle(6));
    
    Console.WriteLine($"There are now {myCircles.Count} circles");
    Console.WriteLine("The radius of my second circle is: " + myCircles[1].Radius);
    
    myCircles.RemoveAt(2);
    Console.WriteLine($"There are now {myCircles.Count} circles");
    
    ShowInformation(myCircles[1]);
    
    • List<Circle> myCircles = new List<Circle>(); declares a List with objects of the Circle class named myCircles.
    • The next three lines fill the List with three objects.
    • myCircles.Count gives the number of objects in the List, which is now three.
    • myCircles[1] gives the second object (a Circle); myCircles[1].Radius gives the radius of that object.
    • myCircles.RemoveAt(2) removes the third object.
    • myCircles.Count gives the number of objects in the List, which is now two.
  3. Run your program. Your output will look like this:

    1
    2
    3
    4
    5
    6
    7
    8
    There are now 3 circles
    The radius of my second circle is: 1.0
    There are now 2 circles
    Color: white
    Perimeter: 6.283185307179586
    Area: 3.141592653589793
    Radius: 1.0
    Center: (0.00, 0.00)