6.2 List
- If needed, start from the project up to chapter 5. This contains the classes as you left them at the end of chapter 5.
-
Add the following code to your
Mainmethod: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 theCircleclass namedmyCircles.- The next three lines fill the List with three objects.
myCircles.Countgives the number of objects in the List, which is now three.myCircles[1]gives the second object (aCircle);myCircles[1].Radiusgives the radius of that object.myCircles.RemoveAt(2)removes the third object.myCircles.Countgives the number of objects in the List, which is now two.
-
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)