Collection Interfaces5:06 with Jeremy McLain
We can make our code easier to use by using collection interfaces whenever possible.
If you take another look at the documentation for the generic 0:00 list collection, you'll notice that it implements a number of interfaces here. 0:03 Every collection type implements at least the IEnumerable interface and 0:08 the ICollection interface. 0:14 These two interfaces define what it means to be a collection. 0:16 As you can see, the list collection also implements all of these other interfaces. 0:20 Including the IList interface, which defines what it means to be a list. 0:26 Let's take a deeper look at what it means for 0:31 a collection to implement these interfaces. 0:33 And how we can use these interfaces to our advantage when working with collections. 0:36 If you need a refresher on interfaces, check the teacher's notes for 0:40 a link to a Treehouse course they can get you up to speed. 0:43 To see a list of other interfaces implemented by collections 0:47 in the System.Collections.Generic name space 0:50 we need to take a look at the documentation for that namespace. 0:53 You'll find a link to this page in the teacher's notes. 0:56 Or you can click-on this link right here. 0:59 You wanna keep this documentation page open for 1:02 the duration of the course because we'll be referring back to it often. 1:05 Let's scroll down to the section on interfaces. 1:08 Here's the IEnumerable interface. 1:13 IEnumerable is one of the interfaces that all collections implement. 1:16 In order to use a foreach loop to loop through items of a collection, 1:20 it must implement the IEnumerable interface. 1:24 It exposes a single method name to get an numerator. 1:27 We'll learn more about IEnumerators and 1:31 the IEnumerable interface in a later course. 1:33 The ICollection interface is another interface that all collections implement. 1:35 It exposes the Count property which tells us how many items are in the collection. 1:40 It also defines Add, Clear, Contains and the Remove methods. 1:45 The Contains method works just like the index of method. 1:51 Only instead of returning the index of the item it only returns true or false. 1:55 Not all collections can be indexed into an array or a list. 2:00 But we can still use the Contains method to determine 2:04 if an item is in the collection. 2:07 Notice that the ICollection interface also inherits from the IEnumerable interface. 2:10 If you implement a collection on your own you can just state that it 2:16 implements ICollection. 2:20 Listing IEnumerable is redundant. 2:22 The IList interface adds one property that the ICollection interface doesn't have. 2:24 This is the Item property here. 2:31 This allows us to use the square brackets to index into the list. 2:33 The IList interface also exposes the IndexOf, Insert, and RemoveAt methods. 2:38 So these are the bare minimum methods and properties that a list provides. 2:44 Of course as we've seen with lists classes that implement the IList interface 2:49 can add additional methods. 2:54 As you can see there are many other Collection interfaces that 2:55 the list doesn't implement. 2:59 We'll learn about these in the rest of this course. 3:01 So why is it important that developers know about 3:03 what each of these interfaces does? 3:06 It's important because we always wanna use the interface whenever possible when we 3:08 specify what type of collection a method takes as a parameter. 3:12 This gives the color of the method the most flexibility 3:16 in what they pass into it. 3:20 I've created a class named SchoolRole. 3:21 That helps to manage all of the students of a school. 3:24 Start a new workspace by clicking on the button on this page to see these changes. 3:27 There is a method named AddStudents that takes a list of students and 3:32 then adds all of the new students to the list of students already in the school. 3:36 The way this method is written right now, 3:40 the color of this method would have to pass the list collection type. 3:43 However, they might already have the students they need to add in an array or 3:47 another type of collection. 3:52 They'd first have to convert their collection to a list before it can be 3:53 passed to the AddStudents method. 3:57 However, the AddStudents method 3:59 doesn't need to use the full functionality of a list. 4:01 It only needs the ability to loop through the new students passed in and 4:05 add them to the SchoolRoll. 4:08 That's actually exactly what the AddRange method does. 4:10 So we really only need to accept an IEnumerable here. 4:14 So we can change List to IEnumerable. 4:19 This allows the caller of the AddStudents method to pass in any type of collection. 4:24 Because all collections implement the IEnumerable interface. 4:29 If the AddStudents method needed to see how many new students were being added, 4:33 we could change IEnumerable to ICollection. 4:38 This would allow us to call the Count property. 4:44 Or if we needed to be able to index into the student's collection 4:47 we could change IENumerable to IList. 4:50 However, by using a more complex interface type we're limiting 4:54 what can be passed into the AddStudents method. 4:58 So we'll keep this as IEnumerable. 5:00
You need to sign up for Treehouse in order to download course files.Sign up