Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Looping through Arrays | Arrays
C# Basics
course content

Course Content

C# Basics

C# Basics

1. Getting Started
2. Dealing with Data Types
3. Control Structures
4. Loops
5. Arrays
6. Methods

bookLooping through Arrays

It is very useful to be able to quickly go through all the elements of an array and perform some operation on them or extract their values.

There are a number of ways we can do that however the most common is using the for and foreach loops.

In the case of using the for loop, we can use the loop's i variable for indexing:

cs

main

copy
12345678910111213141516
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 5, 10, 15, 20, 25 }; for(int i = 0; i < 5; i++) { Console.WriteLine(numbers[i]); } } } }

In the above code we wrote the condition i < 5 where we chose 5 as the limit. Instead of manually counting the number of elements we can also use the Length attribute of the arrays to access the length (size) of an array:

cs

main

copy
12345678910111213
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] myArray = new int[10]; Console.WriteLine(myArray.Length); // Output: 10 } } }

Using the length attribute the loop will look something like this:

cs

main

copy
12345678910111213141516
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 5, 10, 15, 20, 25 }; for(int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); } } } }

We can also output the numbers in reverse by modifying the loop to run in the opposite direction:

cs

main

copy
1234567891011121314151617
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 5, 10, 15, 20, 25 }; for (int i = numbers.Length - 1; i >= 0; i--) { Console.WriteLine(numbers[i]); } } } }

We initiate i with the value numbers.Length - 1, because the index of the last element of an array is 1 less than the size of that array, so in this case the loop variable will start from value 9. The loop condition i >= 0 ensures that the loop continues till i reaches 0. We decrement the value of i every iteration.

Another slightly easier method of looping through arrays is by using the foreach loop. The syntax of a foreach loop is as follows:

cs

main

copy
1234
foreach (datatype variableName in arrayName) { // code block to be executed }

For-example:

cs

main

copy
12345678910111213141516
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 5, 10, 15, 20, 25 }; foreach(int n in numbers) { Console.WriteLine($"Number: {n}"); }; } } }

The foreach loop is simpler than the for loop however the difference is that we cannot modify the original elements of the array in a foreach loop. If we try to assign a new value to n in the above code, the compiler will show an error. However, it is possible in the for loop, since we know the indices of the elements we can modify the array:

cs

main

copy
1234567891011121314151617181920
using System; namespace ConsoleApp { internal class Program { static void Main(string[] args) { int[] numbers = { 5, 10, 15, 20, 25 }; for(int i = 0; i < numbers.Length; i++) { numbers[i] *= 2; }; for(int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); } } } }
What is the correct usage of the foreach loop for the given array?

What is the correct usage of the foreach loop for the given array?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 7
some-alt