1 00:00:00,120 --> 00:00:04,980 ‫And the last year we looked at how the eye innumerable works from a technical point of view. 2 00:00:04,980 --> 00:00:08,660 ‫And this video we are going to check out how we can take advantage of it. 3 00:00:08,670 --> 00:00:17,160 ‫Because the cool thing of I enumerable is, is that the list types that we use usually such as list, 4 00:00:17,190 --> 00:00:23,340 ‫cue array, all of those are in fact implementing the AI enumerable interface. 5 00:00:23,610 --> 00:00:27,750 ‫So this gives us a lot of freedom and we're going to see what we can do with it. 6 00:00:28,080 --> 00:00:33,180 ‫So I'm going to have this method called get collection. 7 00:00:33,570 --> 00:00:39,450 ‫And what it will do is it will return an i enumerable of type integer. 8 00:00:40,560 --> 00:00:42,840 ‫So we will need to return something at one point. 9 00:00:42,990 --> 00:00:45,900 ‫So let's return null for now to get rid of this error here. 10 00:00:46,020 --> 00:00:54,120 ‫And now if you want to use the i enumerable, we can see that we need to add system collections generic 11 00:00:54,120 --> 00:00:59,760 ‫because here we're using integer, we're going to return an immutable rebel integer. 12 00:00:59,760 --> 00:01:04,230 ‫So that means we need to make sure that we have this namespace collections generic. 13 00:01:05,300 --> 00:01:09,150 ‫All right, so now what shall this get collection do? 14 00:01:09,170 --> 00:01:11,870 ‫So basically, we can have multiple options. 15 00:01:11,870 --> 00:01:17,480 ‫So if we select the option one, our get collection will return a list. 16 00:01:17,510 --> 00:01:25,380 ‫If we take the option to it will return a Q and if we take any other options as there's three, four 17 00:01:25,380 --> 00:01:29,660 ‫or five, whatever, it will return an array of integers. 18 00:01:29,660 --> 00:01:37,790 ‫So this get collection method will return some entirely different types of lists depending on the option 19 00:01:37,790 --> 00:01:39,920 ‫that we're going to pass to it. 20 00:01:40,250 --> 00:01:46,070 ‫So that's the idea behind this get collection, and that's also the power of using the I in numeral 21 00:01:46,070 --> 00:01:53,900 ‫interface because as I said, list, queue and array are all implementing the AI enumerable interface. 22 00:01:54,290 --> 00:01:56,000 ‫Okay, so let's look at it. 23 00:01:56,030 --> 00:02:02,780 ‫Let's create this list which I'm going to call numbers list and I'm going to initialize it straight 24 00:02:02,780 --> 00:02:03,080 ‫away. 25 00:02:03,080 --> 00:02:08,840 ‫So it will be a list of integers and the numbers will be one, two, three, four, five, and then 26 00:02:08,840 --> 00:02:14,420 ‫we're going to create a queue with a bunch of values. 27 00:02:14,420 --> 00:02:18,950 ‫So here, six, seven, eight, nine, ten and we're going to include them to this numbers. 28 00:02:18,950 --> 00:02:20,600 ‫Q So you see. 29 00:02:20,630 --> 00:02:21,800 ‫Q Numbers. 30 00:02:21,800 --> 00:02:27,230 ‫Q Now, if you hover over it, you can see it's also a generic collection. 31 00:02:27,830 --> 00:02:29,180 ‫The same goes for the list. 32 00:02:29,210 --> 00:02:31,040 ‫It's a generic collection. 33 00:02:31,090 --> 00:02:33,560 ‫Same goes for the I enumerable generic collection. 34 00:02:33,560 --> 00:02:36,290 ‫So you can see there is a certain collection already. 35 00:02:36,290 --> 00:02:42,140 ‫So now what I'm going to do is I'm going to check what are the options that are taken. 36 00:02:42,140 --> 00:02:44,810 ‫So what is past to my collection method? 37 00:02:45,020 --> 00:02:49,190 ‫If it is one, then I'm going to return my numbers list. 38 00:02:49,730 --> 00:02:54,710 ‫If it's going to be two, I'm going to return my numbers. 39 00:02:54,710 --> 00:02:59,030 ‫Q So an entirely different type of list. 40 00:02:59,480 --> 00:03:06,140 ‫So it's containing different type of information and different values, but it's really just an entirely 41 00:03:06,140 --> 00:03:07,070 ‫different thing. 42 00:03:07,430 --> 00:03:11,180 ‫So it's not a list, it's a Q and a Q is not a type of A list. 43 00:03:11,180 --> 00:03:14,210 ‫So Q is not inheriting from list or something like that. 44 00:03:15,110 --> 00:03:19,490 ‫So that's really the interesting thing here, what we can do and otherwise. 45 00:03:19,490 --> 00:03:26,060 ‫So if the option is anything else, we're going to have this LS case where we are just going to return 46 00:03:26,930 --> 00:03:29,030 ‫1112, 13, 14, 50. 47 00:03:30,260 --> 00:03:34,310 ‫Now in this case, you can see this return statement here is never going to be reached. 48 00:03:34,310 --> 00:03:40,850 ‫So we can get rid of this line here entirely because it's basically never going to be reached if we 49 00:03:40,850 --> 00:03:44,510 ‫have this, if else, statement with a bunch of returns here. 50 00:03:47,110 --> 00:03:47,650 ‫All right. 51 00:03:47,650 --> 00:03:52,440 ‫So now that we have this get collection method, let's actually use it in our main method. 52 00:03:52,450 --> 00:03:58,180 ‫So here in the starting point of our application, let's create an unknown collection. 53 00:03:58,180 --> 00:04:06,520 ‫So just an I enumerable collection that is going to be a generic collection that uses integers or holds 54 00:04:06,520 --> 00:04:12,340 ‫integers, and we will use this variable to store the collection that will get returned by the get collection 55 00:04:12,340 --> 00:04:12,700 ‫method. 56 00:04:12,700 --> 00:04:20,490 ‫So by this method that we just created, now let's actually just assign get collection one to it. 57 00:04:20,590 --> 00:04:26,350 ‫So because we passed one to it, unknown collection will now hold. 58 00:04:27,540 --> 00:04:28,730 ‫Uh, let's look here. 59 00:04:28,980 --> 00:04:31,910 ‫The list here, this list here. 60 00:04:31,920 --> 00:04:33,780 ‫Not the queue, but the list. 61 00:04:34,650 --> 00:04:36,060 ‫So that's a really cool thing. 62 00:04:36,120 --> 00:04:44,820 ‫So now we can display this on our console, saying for each number in the collection, we got back from 63 00:04:44,820 --> 00:04:46,200 ‫the get collection. 64 00:04:46,380 --> 00:04:54,000 ‫So we're going to use the for each in order to get all the numbers that are in our unknown collection. 65 00:04:54,090 --> 00:05:03,930 ‫Because if something is an AI enumerable, so if a list type like a list like an array, like an Q is 66 00:05:03,930 --> 00:05:12,270 ‫of type or is implementing the AI enumerable interface, then we can iterate through it using it for 67 00:05:12,270 --> 00:05:14,150 ‫each loop, which is what we're doing here. 68 00:05:14,160 --> 00:05:19,920 ‫So we're going to go through this unknown collection and for each number we're just going to write it 69 00:05:19,920 --> 00:05:21,150 ‫on to the console. 70 00:05:21,750 --> 00:05:23,250 ‫So let's run this real quick. 71 00:05:24,520 --> 00:05:28,090 ‫And we can see one, two, three, four, five was printed out. 72 00:05:28,570 --> 00:05:30,190 ‫So that seemed to have worked. 73 00:05:30,430 --> 00:05:32,350 ‫Now let's do the same thing. 74 00:05:32,350 --> 00:05:34,990 ‫But with collection too. 75 00:05:35,320 --> 00:05:41,170 ‫So get collection and we pass the option of two, which will now return to Q. 76 00:05:42,040 --> 00:05:47,380 ‫But it will store it in the base type of our generic collections. 77 00:05:48,150 --> 00:05:48,390 ‫Okay. 78 00:05:48,390 --> 00:05:58,020 ‫So unknown collection is the base type I enumerable, which is basically the base type of our list and 79 00:05:58,020 --> 00:05:58,680 ‫our. 80 00:05:58,680 --> 00:06:05,910 ‫Q So now we can just assign it to it because unknown collection doesn't care if it's a list, if it's 81 00:06:05,910 --> 00:06:10,050 ‫a Q, if it's an array, because all of them are. 82 00:06:10,900 --> 00:06:17,860 ‫Following the instructions of an AI enumerable, so they followed the implementation of it. 83 00:06:18,250 --> 00:06:23,350 ‫So now if we run this code, now it will display. 84 00:06:25,140 --> 00:06:27,390 ‫The queue here six, seven, eight, nine, ten. 85 00:06:27,390 --> 00:06:31,770 ‫So this was a queue int and it displays it for us. 86 00:06:34,360 --> 00:06:34,690 ‫All right. 87 00:06:34,690 --> 00:06:39,530 ‫And now let's look at this if we enter any other type of option. 88 00:06:39,550 --> 00:06:46,990 ‫So let's say we give it the option five, which will be returning this case here, which basically will 89 00:06:46,990 --> 00:06:49,900 ‫return an array of numbers. 90 00:06:50,650 --> 00:06:52,060 ‫So let's run this again. 91 00:06:53,120 --> 00:06:58,370 ‫And we will see that now it returns 11, 12, 13, 14, 15. 92 00:06:58,730 --> 00:07:01,800 ‫So, of course, these were the ones from before. 93 00:07:01,820 --> 00:07:03,140 ‫So these for each loops. 94 00:07:03,680 --> 00:07:04,730 ‫We didn't delete them. 95 00:07:04,730 --> 00:07:07,160 ‫Of course, we could get rid of them to see that. 96 00:07:07,160 --> 00:07:11,930 ‫Now it only will do the array or display the array. 97 00:07:12,080 --> 00:07:14,120 ‫So there we are. 98 00:07:14,120 --> 00:07:15,740 ‫It only displays the array. 99 00:07:16,040 --> 00:07:16,460 ‫That's it. 100 00:07:16,460 --> 00:07:23,930 ‫For this example, you see that we have quite an advantage of using i enumerable so the base type, 101 00:07:23,930 --> 00:07:31,760 ‫so to speak, of any type of generic list that we want to make iterable like the for each loop well 102 00:07:31,760 --> 00:07:33,790 ‫e for each loop label so to speak. 103 00:07:33,800 --> 00:07:36,320 ‫And that's really the power of it. 104 00:07:36,320 --> 00:07:37,760 ‫Really useful stuff. 105 00:07:37,910 --> 00:07:43,370 ‫And the next video, we're going to look at another example where we're going to pass an I enumerable 106 00:07:43,370 --> 00:07:45,830 ‫collection as a parameter for a method. 107 00:07:46,370 --> 00:07:46,910 ‫All right. 108 00:07:46,910 --> 00:07:48,650 ‫So that's it for this video. 109 00:07:48,680 --> 00:07:49,640 ‫See you in the next one.