1 00:00:05,240 --> 00:00:06,265 Hello and welcome. 2 00:00:06,265 --> 00:00:08,239 I'm Jeremy, in this workshop, 3 00:00:08,239 --> 00:00:13,200 we'll see how to use generics to create our own types of data structures. 4 00:00:13,200 --> 00:00:17,160 Data structures are classes that help to organize and use data efficiently. 5 00:00:17,160 --> 00:00:21,985 For example, lists and dictionary are data structures provided by the .Net Framework. 6 00:00:21,985 --> 00:00:26,110 The .Net Framework refers to these data structures as collections 7 00:00:26,110 --> 00:00:29,870 because they're used to organize collections of things. 8 00:00:29,870 --> 00:00:34,290 List and dictionary are also examples of generic classes. 9 00:00:34,290 --> 00:00:38,120 You probably remember from previous courses that we can specify 10 00:00:38,120 --> 00:00:42,630 what type of object we want a list to contain by putting the name of the type 11 00:00:42,630 --> 00:00:46,520 between these two angle brackets when we instantiate the list. 12 00:00:46,520 --> 00:00:49,020 This is called the generic type parameter. 13 00:00:49,020 --> 00:00:52,250 And it's what makes list a generic class. 14 00:00:52,250 --> 00:00:56,690 We use generic classes like list all the time, but in order to really understand 15 00:00:56,690 --> 00:01:00,410 how generics and collections work, it's best to implement our own. 16 00:01:00,410 --> 00:01:03,720 In fact, writing your own data structures to help you work with 17 00:01:03,720 --> 00:01:06,850 data can help keep your code clean and readable. 18 00:01:06,850 --> 00:01:10,400 You'll find that data structures you write to help you solve one problem 19 00:01:10,400 --> 00:01:14,620 can often be reused the next time you run into a similar problem. 20 00:01:14,620 --> 00:01:18,160 Coding them as generic classes makes them even more usable. 21 00:01:19,370 --> 00:01:22,820 Let's start writing our own generic data structure. 22 00:01:22,820 --> 00:01:27,040 I'll be writing the code for this workshop using Visual Studio. 23 00:01:27,040 --> 00:01:30,570 If you're new to Visual Studio, you can get familiar with it by 24 00:01:30,570 --> 00:01:34,030 following the link to the tree house workshop in the teacher's notes. 25 00:01:34,030 --> 00:01:36,940 Of course, you don't have to use Visual Studio to code in C-sharp. 26 00:01:36,940 --> 00:01:39,740 You can use any editor you want or 27 00:01:39,740 --> 00:01:43,910 even open a Treehouse workspace by clicking on the button on this page. 28 00:01:43,910 --> 00:01:45,440 Let's get going. 29 00:01:45,440 --> 00:01:49,620 I'm in Visual Studio and I've created a console application project. 30 00:01:49,620 --> 00:01:52,484 I've named this project GenericsDemo. 31 00:01:52,484 --> 00:01:57,619 In program.cs, I've created four different collections of integers, 32 00:01:57,619 --> 00:02:00,520 two lists, a HashSet and an array. 33 00:02:00,520 --> 00:02:03,270 Let's say we wanted to count all of the odd 34 00:02:03,270 --> 00:02:06,000 numbers contained in these four collections. 35 00:02:06,000 --> 00:02:07,720 How could we do that? 36 00:02:07,720 --> 00:02:11,870 I've already written a method to determine if a value is odd or not. 37 00:02:11,870 --> 00:02:16,230 Now for each collection, we can loop through each value in the array and 38 00:02:16,230 --> 00:02:17,470 check if it's odd. 39 00:02:17,470 --> 00:02:20,980 We'll increment a counter for each odd number we find. 40 00:02:20,980 --> 00:02:22,760 Let's see what the code for this looks like. 41 00:02:24,820 --> 00:02:28,630 So I'll start by making an integer variable and 42 00:02:28,630 --> 00:02:32,450 I'll name it numOdd, I'll set it equal to 0. 43 00:02:34,360 --> 00:02:38,500 This will help us keep track of how many odd numbers we've encountered. 44 00:02:38,500 --> 00:02:43,927 Now I'll say, foreach var value in list1. 45 00:02:49,200 --> 00:02:53,819 If, and then we'll call the IsOdd method and pass in value. 46 00:02:56,890 --> 00:03:03,106 So if it's odd, we'll increment numOdd, so I'll say numOdd ++. 47 00:03:05,049 --> 00:03:08,510 Now we just need to do this with the other three collections. 48 00:03:08,510 --> 00:03:13,695 So I'll copy this code, And 49 00:03:13,695 --> 00:03:17,930 I'll paste it and each time, I'll change it to the next collection name. 50 00:03:17,930 --> 00:03:20,330 So this one will be list2. 51 00:03:25,649 --> 00:03:27,830 I'm going to do this two more times. 52 00:03:27,830 --> 00:03:30,203 So this one is for set1. 53 00:03:35,495 --> 00:03:37,752 And the last one is for array1. 54 00:03:44,590 --> 00:03:45,340 There we go. 55 00:03:45,340 --> 00:03:48,020 So this code loops through each collection and 56 00:03:48,020 --> 00:03:52,160 increments numOdd for each odd number that it finds. 57 00:03:52,160 --> 00:03:56,030 We could do anything we wanted with each value in these collections. 58 00:03:56,030 --> 00:03:58,830 But we'd have to repeat it inside of each loop. 59 00:03:58,830 --> 00:04:00,540 There's got to be a better way. 60 00:04:00,540 --> 00:04:04,150 We could shorten the substantially using LINQ. 61 00:04:04,150 --> 00:04:05,805 If you're unfamiliar with LINQ, 62 00:04:05,805 --> 00:04:09,180 you'll find a link to a great course about it in the teacher's notes. 63 00:04:09,180 --> 00:04:13,720 So to use LINQ, I'll first have to go up here and add a using directive. 64 00:04:13,720 --> 00:04:16,667 So I'll say using System.LINQ. 65 00:04:19,780 --> 00:04:24,545 This allows us access to all of the extension methods defined in LINQ. 66 00:04:25,710 --> 00:04:27,305 Now it's write our LINQ expression. 67 00:04:31,055 --> 00:04:34,755 Our LINQ expression will replace all of these foreach loops. 68 00:04:34,755 --> 00:04:41,754 So I'll just say numOdd equals list1.Count. 69 00:04:42,840 --> 00:04:46,630 So here I'll write a lambda expression where x is the value. 70 00:04:46,630 --> 00:04:54,300 So say x, for each x call IsOdd and pass an x. 71 00:04:56,320 --> 00:05:00,060 We'll call Count on each collection and add up the results. 72 00:05:02,060 --> 00:05:05,272 So this is for list2. 73 00:05:07,942 --> 00:05:09,677 And set1, 74 00:05:15,142 --> 00:05:18,817 And array1. 75 00:05:18,817 --> 00:05:21,034 That's certainly less code, but 76 00:05:21,034 --> 00:05:25,970 we're still repeating the calls to count and IsOdd four times. 77 00:05:25,970 --> 00:05:29,730 What we'd really like to be able to do is loop through these collections 78 00:05:29,730 --> 00:05:32,090 as if they're all one collection. 79 00:05:32,090 --> 00:05:35,840 It's not unreasonable to think that this is something that would be useful for 80 00:05:35,840 --> 00:05:37,650 more than just this case. 81 00:05:37,650 --> 00:05:42,190 So we should write some code that does this in a reusable way. 82 00:05:42,190 --> 00:05:45,840 To do this, we'll write our own data structure that takes a bunch of 83 00:05:45,840 --> 00:05:49,200 collections and makes them behave like a single collection. 84 00:05:49,200 --> 00:05:52,450 We can make it a generic collection so that it can be used for 85 00:05:52,450 --> 00:05:57,560 not just integers, but any type of object that a collection can contain. 86 00:05:57,560 --> 00:06:01,110 We'll call this new collection type and an enumerable composite. 87 00:06:01,110 --> 00:06:03,750 And we'll start coding it in the next video. 88 00:06:03,750 --> 00:06:07,440 While we make this new collection type, we'll also be learning the ins and 89 00:06:07,440 --> 00:06:12,365 outs of generics as well as how to implement the IEnumerable interface. 90 00:06:12,365 --> 00:06:15,980 IEnumerable is the interface that every collection type implements.