Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Array Iteration in C#

Introduction

In C#, arrays are a collection of items stored at contiguous memory locations. They are used to store multiple values in a single variable. Iterating through arrays is a fundamental concept in programming that allows you to access and manipulate each element of an array. In this tutorial, we will explore various methods to iterate through arrays in C#.

For Loop

The for loop is one of the most common ways to iterate through an array. It allows you to execute a block of code a specified number of times.


int[] numbers = {1, 2, 3, 4, 5};
for (int i = 0; i < numbers.Length; i++) {
    Console.WriteLine(numbers[i]);
}
                

Explanation:

In the example above, we initialize an array called numbers. The for loop starts with i = 0 and runs until i is less than the length of the array. During each iteration, the current element of the array is printed to the console.

Foreach Loop

The foreach loop is a simpler and more readable way to iterate through arrays. It is specifically designed for collections and arrays.


int[] numbers = {1, 2, 3, 4, 5};
foreach (int number in numbers) {
    Console.WriteLine(number);
}
                

Explanation:

In the example above, the foreach loop iterates through each element of the numbers array. The loop variable number represents the current element of the array during each iteration.

While Loop

The while loop can also be used to iterate through an array. It repeats a block of code as long as a specified condition is true.


int[] numbers = {1, 2, 3, 4, 5};
int i = 0;
while (i < numbers.Length) {
    Console.WriteLine(numbers[i]);
    i++;
}
                

Explanation:

In the example above, we use a while loop to iterate through the numbers array. The loop continues as long as i is less than the length of the array. During each iteration, the current element is printed to the console, and i is incremented.

Do-While Loop

The do-while loop is similar to the while loop, but it guarantees that the loop body will be executed at least once.


int[] numbers = {1, 2, 3, 4, 5};
int i = 0;
do {
    Console.WriteLine(numbers[i]);
    i++;
} while (i < numbers.Length);
                

Explanation:

In the example above, we use a do-while loop to iterate through the numbers array. The loop body is executed first, and then the condition is checked. If the condition is true, the loop continues.

Using LINQ

Language Integrated Query (LINQ) provides a powerful way to perform operations on arrays and other collections. The foreach method in LINQ can be used to iterate through an array.


using System;
using System.Linq;

int[] numbers = {1, 2, 3, 4, 5};
numbers.ToList().ForEach(number => Console.WriteLine(number));
                

Explanation:

In the example above, we convert the numbers array to a list using ToList(), and then use the ForEach method to iterate through each element, printing it to the console.

Conclusion

In this tutorial, we covered various methods to iterate through arrays in C#. We explored the for loop, foreach loop, while loop, do-while loop, and using LINQ. Each method has its own use case and advantages, and understanding these will help you write more efficient and readable code.