‫And the last year we looked at how the eye innumerable works from a technical point of view.
‫And this video we are going to check out how we can take advantage of it.
‫Because the cool thing of I enumerable is, is that the list types that we use usually such as list,
‫cue array, all of those are in fact implementing the AI enumerable interface.
‫So this gives us a lot of freedom and we're going to see what we can do with it.
‫So I'm going to have this method called get collection.
‫And what it will do is it will return an i enumerable of type integer.
‫So we will need to return something at one point.
‫So let's return null for now to get rid of this error here.
‫And now if you want to use the i enumerable, we can see that we need to add system collections generic
‫because here we're using integer, we're going to return an immutable rebel integer.
‫So that means we need to make sure that we have this namespace collections generic.
‫All right, so now what shall this get collection do?
‫So basically, we can have multiple options.
‫So if we select the option one, our get collection will return a list.
‫If we take the option to it will return a Q and if we take any other options as there's three, four
‫or five, whatever, it will return an array of integers.
‫So this get collection method will return some entirely different types of lists depending on the option
‫that we're going to pass to it.
‫So that's the idea behind this get collection, and that's also the power of using the I in numeral
‫interface because as I said, list, queue and array are all implementing the AI enumerable interface.
‫Okay, so let's look at it.
‫Let's create this list which I'm going to call numbers list and I'm going to initialize it straight
‫away.
‫So it will be a list of integers and the numbers will be one, two, three, four, five, and then
‫we're going to create a queue with a bunch of values.
‫So here, six, seven, eight, nine, ten and we're going to include them to this numbers.
‫Q So you see.
‫Q Numbers.
‫Q Now, if you hover over it, you can see it's also a generic collection.
‫The same goes for the list.
‫It's a generic collection.
‫Same goes for the I enumerable generic collection.
‫So you can see there is a certain collection already.
‫So now what I'm going to do is I'm going to check what are the options that are taken.
‫So what is past to my collection method?
‫If it is one, then I'm going to return my numbers list.
‫If it's going to be two, I'm going to return my numbers.
‫Q So an entirely different type of list.
‫So it's containing different type of information and different values, but it's really just an entirely
‫different thing.
‫So it's not a list, it's a Q and a Q is not a type of A list.
‫So Q is not inheriting from list or something like that.
‫So that's really the interesting thing here, what we can do and otherwise.
‫So if the option is anything else, we're going to have this LS case where we are just going to return
‫1112, 13, 14, 50.
‫Now in this case, you can see this return statement here is never going to be reached.
‫So we can get rid of this line here entirely because it's basically never going to be reached if we
‫have this, if else, statement with a bunch of returns here.
‫All right.
‫So now that we have this get collection method, let's actually use it in our main method.
‫So here in the starting point of our application, let's create an unknown collection.
‫So just an I enumerable collection that is going to be a generic collection that uses integers or holds
‫integers, and we will use this variable to store the collection that will get returned by the get collection
‫method.
‫So by this method that we just created, now let's actually just assign get collection one to it.
‫So because we passed one to it, unknown collection will now hold.
‫Uh, let's look here.
‫The list here, this list here.
‫Not the queue, but the list.
‫So that's a really cool thing.
‫So now we can display this on our console, saying for each number in the collection, we got back from
‫the get collection.
‫So we're going to use the for each in order to get all the numbers that are in our unknown collection.
‫Because if something is an AI enumerable, so if a list type like a list like an array, like an Q is
‫of type or is implementing the AI enumerable interface, then we can iterate through it using it for
‫each loop, which is what we're doing here.
‫So we're going to go through this unknown collection and for each number we're just going to write it
‫on to the console.
‫So let's run this real quick.
‫And we can see one, two, three, four, five was printed out.
‫So that seemed to have worked.
‫Now let's do the same thing.
‫But with collection too.
‫So get collection and we pass the option of two, which will now return to Q.
‫But it will store it in the base type of our generic collections.
‫Okay.
‫So unknown collection is the base type I enumerable, which is basically the base type of our list and
‫our.
‫Q So now we can just assign it to it because unknown collection doesn't care if it's a list, if it's
‫a Q, if it's an array, because all of them are.
‫Following the instructions of an AI enumerable, so they followed the implementation of it.
‫So now if we run this code, now it will display.
‫The queue here six, seven, eight, nine, ten.
‫So this was a queue int and it displays it for us.
‫All right.
‫And now let's look at this if we enter any other type of option.
‫So let's say we give it the option five, which will be returning this case here, which basically will
‫return an array of numbers.
‫So let's run this again.
‫And we will see that now it returns 11, 12, 13, 14, 15.
‫So, of course, these were the ones from before.
‫So these for each loops.
‫We didn't delete them.
‫Of course, we could get rid of them to see that.
‫Now it only will do the array or display the array.
‫So there we are.
‫It only displays the array.
‫That's it.
‫For this example, you see that we have quite an advantage of using i enumerable so the base type,
‫so to speak, of any type of generic list that we want to make iterable like the for each loop well
‫e for each loop label so to speak.
‫And that's really the power of it.
‫Really useful stuff.
‫And the next video, we're going to look at another example where we're going to pass an I enumerable
‫collection as a parameter for a method.
‫All right.
‫So that's it for this video.
‫See you in the next one.