Hey, everybody.
In this video, we’ll be going over a new type of loop which is called a foreach loop.
This for loop is pretty much designed to help us work and deal with collections of data.
An array is a collection, and we can loop through it using foreach loop.
You might have a question as to what the difference is between a for loop and a foreach loop.
And it’s a wonderful question, which is why I brought back the old example from last
time.
int[] array = new int[5]; array[0] = 1;
array[1] = 10; array[2] = 100;
array[3] = 1000; array[4] = 10000;
for (int index = 0; index < array.Length; index++)
{ Console.WriteLine(array[index]);
} In the last tutorial, we learned how to loop
through an array using a for loop.
Now I will write a foreach loop right next to our for loop so that I can go over the
similarities and differences.
If we type foreach and press Tab twice, it generates the structure automatically.
foreach (var VARIABLE in COLLECTION) {
} You know about the var reserved keyword.
It recognizes what type of value is being assigned to it, and it does it on run time,
so you don’t have to worry about it.
It’s pretty convenient.
VARIABLE can be anything you want it to be.
For the sake of this example, let’s call it element because a single item in our array
is an element.
So we’re going to call it an element.
foreach (var element in COLLECTION) {
} VARIABLE is an alias.
It can be anything.
Then you have another reserved keyword in.
After that, you can see it’s underlined red and it says COLLECTION.
This is where you have to put your collection.
In this case, it’s going to be array.
Why?
Because we named it array.
Whatever the name of our collection is, it’s going to be here.
foreach (var element in array) {
} What this is it will go through each element
in the specified collection from beginning all the way to the end.
So that’s the purpose of a foreach loop.
The purpose is to go through a collection from start to finish.
We can do the same thing as we did for for loop.
But notice one thing.
We don’t have an index.
In a for loop, we have an index.
So if we want to, we can go backwards in the loop.
For some weird reason, if we want to go backwards or we want to manipulate the index, or we
want to jump through instead of doing index++ inside of it, we can do index+=5 and jump
5 over and skip 5 elements.
We can do that if we want to with a for loop.
We have an index.
But with a foreach loop, as you can see, it’s much more concise the way we write it.
All you have to do is specify a name for an individual item that will be retrieved from
the collection that you put on the right side.
And then you have to have in here.
That’s the reserved keyword that you have to have.
And you have to have the type for your variable.
In this case, for the type, we just say var because it makes it a lot easier for us.
We don’t have to know the type.
But if you want to go explicit, you can just say the actual type, which in this case is
int.
But I will keep it var for now.
In Console.Writeline(), we don’t have the index, so we put element in it because that’s
the individual item from the collection being retrieved.
In this case, foreach loop, we’ll do the same exact thing that our for loop will do.
foreach (var element in array) {
Console.WriteLine(element); }
So the bottom line is when you don’t want to know about the index, and where you’re
at currently, you just want to go from start to finish, you use foreach because it’s
much more concise.
It’s much more readable.
And it’s easier on your side.
You don’t have to have any conditions.
You don’t have to have any incrementors.
You don’t have to declare a variable.
Well, you do.
In this case, it’s the individual element named element that is being retrieved from
the collection.
It depends on your situation, but it’s good to know these things so that you can use them
when appropriate.
That’s it.
If you like the video, please give it a thumbs up.
And if you haven’t subscribed, please do so that I can keep updating you with the latest
content.