Join Anton Delsink for an in-depth discussion in this video IEnumerable, part of Object Oriented Programming with C#.
- In this example, we look at code from the .NET framework itself, so we don't need external libraries or other components. We can just go File, New Project, and create a New Project in our Exercises folder. In this case, I will use the .NET Core but the same would work in .NET Desktop for the Full .NET Framework. And I'll pick MSTest Test Project. I'll write all of our code as UnitTests. I'll call the project Tests and I'll end the solution .NET Framework.
Click OK and I'll rename the file UnitTestOne, to Tests_NetFramework. And within that file, I already named the method Test001. And so within this file, we'll create multiple unit tests as we work through the example. So the first example is all about i -- enumerable, the ability to go through all of the elements within a collection one at a time.
And so, the most obvious collection, most of you are already familiar with, is the basic idea of a list of numbers, a sequence of numbers. And we'll say, New, List of Int. And so, List is from the generics name space. So I'll hit the Control dot and so using System dot Collections dot Generic, it behaves very similar to an array, but of course we are talking about a list which has a dynamic length, but each element in this case is of type integer and so if this is a new concept to you it's well worth looking at the ealrier courses, while the C-Sharp syntax, and especially the part of those courses that deal with generic types.
In this case List is a generic collection, generic in the sense that it can be a list of any specified type, and in this example we use a list of integers. And so, we're going to be talking about how we go through the collection of elements in this list, and so the first thing to confirm using the Assert statement is True. Is whether numbers is in fact Inumerable And so, this a question we are asking ourself, in that is the numbers object that we enstantiating here actually enumberable, so I'm asking the compiler to confirm whether I can use the ForEach keyword.
And you're all expecting me to write something like this ForEach Var N, some number in the collection numbers and so then we are looping through we're iterating over all of the elements in this collection. But for that to be possible, a lot is happening behind the scenes. So what exactly is happening? Well, first we need to get the thing, the object that makes it possible to go through all of the elements in this collection. We ask the numbers object to provide us with a new object and enumerator that can enumerate the collection, and what is that enumerator? E, the enumerator is (keyboard typing) enumerator of integer.
Now that the language is close here, so we're saying numbers, as a collection is enumerable. It can be enumerated What will do the enumerating? The enumerator, the object we name as the variable, E. And you'll notice the compiler can tell us already by hovering your cursor over the green highlight here, the given expression is always of the provided type. Get enumerator returns an inumerator of T. In this case, T is an integer. So now that we have an enumerator, we can ask it to actually walk us through the elements in this collection, and so the first question really is, whether there is any data at all.
So we can ask, Get Enumerator, MoveNext, tell me if there is data. Now, given the numbers I've typed into the initialization on Line 12, there should be multiple entries and so HasData should be True. So we say, Get Enumerator dot MoveNext and if that returns True, there is data. Well where is the data? It's on that enumerator in a property called Current.
And so, I the integer, is the current value of E dot current must be equal to the first value we got from the list of integers. Control, Shift, B to Build. Now go to the Test Explorer Window, find my test, Right-Click, Run. And it comes back True. And so, the point here was to show that the numbers collection, and there are many kinds, this case it's a list of integers, can provide an enumerator.
How do we know that? Because it implements the interface I enumerable, and because it implements I enumberable, it will have a GetEnumerator method, which gives us the numerator object, that numerator object will go through each element in the collection one at a time and we say MoveNext, to go from the first to the second to the third and onwards. Now we do call MoveNext as the first step to find out if the collection is empty or not. And so, when you call MoveNext the first time, if it comes back True, there is data, and you find that data in the property called Current.
And so, we were to repeat Lines 19 through 23 repeatedly until MoveNext returns False. And that is the basis of the ForEach keyword. And that is how the ForEach keyword is in fact implemented behind the scenes.
- Abstract classes
- Processing text with StreamReader
- IEnumerable and yield return
- Windows Forms controls
- Windows Forms with and without the designer
- Adding, organizing, and testing forms
- Creating classes