Collection Initialization8:59 with Jeremy McLain
There are a few ways to initialize a collection.
One thing we haven't done yet is to provide some mechanism 0:00 to store the objects that are contained in our collection. 0:03 Let's do that now. 0:06 EnumerableCompositor allows treating a bunch of collections as if they're 0:08 one collection. 0:12 Now, we could do this a number of ways. 0:13 One way is to copy all of the items out of each collection and 0:16 insert them into a separate list contained in EnumerableCompositor. 0:20 To do that, we need to first create a private field for the list. 0:25 So I'll just say private, 0:29 List of T and just call it items. 0:34 Now we need to provide some way for the items to get into the list. 0:41 Many collection types allow initializing it with an initial set of 0:45 items using a constructor. 0:49 Our constructor would allow passing in a bunch of collections all at once. 0:51 In order to be as flexible as possible and allow passing in any type of collection, 0:56 we'll want to accept anything that implements the IEnumerable interface. 1:02 So for our constructor here, 1:07 I'll just say public EnumerableCompositor and 1:10 it will take an IEnumerable of IEnumerables of T. 1:17 And we just call it collections. 1:23 Notice how generic types can be nested here. 1:29 What this is saying is that we can pass in a list of arrays, or 1:32 an array of lists, or a set of arrays, or a list of sets. 1:38 So long as they're both IEnumerable, we can pass them in. 1:45 Inside of the constructor, we could have nested foreach loops that loop 1:49 through each of the items in each of the collections and 1:53 then add them to our list of items up there. 1:56 However, the main reason we're writing the EnumerableCompositor class 2:00 is to make it so that we can treat a bunch of collections as a single collection 2:04 with minimal overhead. 2:09 Copying all of the items into a separate list is a lot of overhead. 2:11 We could make it so 2:16 that we just copy references to each collection that's passed in. 2:17 That way we don't need to copy each item. 2:21 We can change our list up here to be a list of IEnumerables of T. 2:24 So say, List IEnumerable of T. 2:31 This has to be a list because we need to be able to add to it. 2:38 So now we aren't actually storing the items contained in the collections. 2:42 We're just storing references to the collections themselves. 2:46 This just goes to show that even though EnumerableCompositor is a collection of T, 2:50 it doesn't need to store objects of type T directly. 2:56 There are some implications of doing it this way that we'll discuss in a bit. 2:59 Now in the constructor, we'll just call ToList on the collection 3:03 of collections passed in the constructor to initialize our collections field. 3:08 And actually, to make what this field is a little bit clearer, 3:13 let's rename it to just collections. 3:17 Now we can say, collections 3:22 equals collections.ToList. 3:27 ToList is provided by link, so we'll need to add a using directive up here. 3:32 So we'll say using System.Link. 3:37 Let's see how to use this constructor. 3:44 In Main we can instantiate an EnumerableCompositor and 3:47 pass in an array of collections. 3:51 So we'll say var ec = new EnumerableCompositor of int. 3:54 And pass in a array, so we'll say new 4:04 IEnumerable of T of int rather array and 4:09 now here we can list our collections. 4:14 So we'll say list1, list2, 4:20 set1, and array1. 4:25 These collections, not the items in them, 4:31 will be copied into the list inside the EnumerableCompositor instance. 4:35 EnumerableCompositor simply stores references to these collections. 4:39 This means that if an item is later added to say list1, it would also appear to 4:45 automatically be added to EnumerableCompositor that contains list1. 4:51 This is why I chose to name this class EnumerableCompositor because it's composed 4:56 of a bunch of enumerables and not the items themselves. 5:01 And because EnumerableCompositor implements IEnumerable, we can use 5:04 foreach loops to loop through or enumerate all of the items contained in it. 5:09 Well, we will be able to after we finish writing the GetEnumerator methods. 5:15 Foreach loops use a special type of object 5:20 called an enumerator to loop through items in a collection. 5:22 And that's what the GetEnumerator methods return. 5:26 Enumerators are sometimes called iterators and we'll see how to implement them soon. 5:31 Before we do that, I'd like to show another way to initialize a collection. 5:36 We saw how to do it using a constructor, but you're probably 5:42 also familiar with using a collection initializer to initialize a list. 5:46 That's when we list all of the items that the collection should be initialized with 5:51 between curly braces. 5:56 This is how we're initializing these four collections up here. 5:59 We can allow the same thing in our collection. 6:05 In order for a collection to be initialized in this way, it must implement 6:08 the IEnumerable interface and it must also have a method named add. 6:13 The add method isn't part of the IEnumerable interface but 6:19 C# knows to look for it if we're trying to initialize a collection like this. 6:24 The neat thing about the add method is that the parameter passed into it 6:29 doesn't need to be the same as the type specified 6:33 in the generic parameter in the collection. 6:36 It happens to be the case that a list of integers 6:39 only allows putting integers in the collection initialization list. 6:42 In our case, we want to do what we have here. 6:47 Where we're listing the collections that we want EnumerableCompositor to be 6:51 composed of. 6:55 So typically, we'd have an add method that looks something like this. 6:56 We'd say public void Add T item, and 7:01 in here we 'dadd the item to the internal collection. 7:06 In our case, we'll change the parameter type to IEnumerable of 7:15 T And 7:20 we'll change the parameter name to collection 7:28 In here, we'll just add the collection to our list of collections. 7:39 So, I'll say _collections.Add(collection). 7:43 Now we can go back to main and uncomment this line. 7:54 We'll get rid of this line we'll recall in the constructor. 8:01 We still have red squiggly lines though, 8:05 these are telling us that we don't have a constructor that takes no parameters. 8:09 We'll need to add that. 8:13 So go back here to EnumerableCompositor. 8:15 And we'll write a constructor that has no parameters. 8:21 So I'll say public EnumerableCompositor with no parameters. 8:23 And we'll need to initialize the list to an empty list instance. 8:30 So, we'll just say _collections = 8:35 new List<IEnumerable<T >> ();. 8:40 There we go. 8:44 Now we can use a collection initialization list to initialize 8:46 the EnumerableCompositor. 8:50 Now that we have a way to store stuff in our collection 8:52 we can finish implementing the GetEnumerator methods. 8:55
You need to sign up for Treehouse in order to download course files.Sign up