CHAPTER 9 COLLECTIONS AND GENERICS
Understanding Collection Initialization Syntax
In Chapter 4, you learned about object initialization syntax, which allows you to set properties on a new
variable at the time of construction. Closely related to this is collection initialization syntax. This C#
language feature makes it possible to populate many containers (such as ArrayList or List) with
items by using syntax similar to what you use to populate a basic array.
Note You can apply collection initialization syntax only to classes that support an Add() method, which is
formalized by the ICollection/ICollection interfaces.
Consider the following examples:
// Init a standard array.
int[] myArrayOfInts = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Init a generic List<> of ints.
List myGenericList = new List { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Init an ArrayList with numerical data.
ArrayList myList = new ArrayList { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
If your container is managing a 6