Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do you use 'foreach' statement for accessing array elements in C#
The foreach statement in C# provides a simple way to iterate through all elements in an array without needing to manage index variables. Unlike a traditional for loop, foreach automatically accesses each element in sequence.
Syntax
Following is the syntax for using foreach with arrays −
foreach (dataType variable in arrayName) {
// code to process each element
}
The foreach loop automatically iterates through each element, assigning the current element's value to the specified variable.
Using foreach with Arrays
Example
using System;
class MyArray {
static void Main(string[] args) {
int[] n = new int[10];
// Initialize elements of array n
for (int i = 0; i < 10; i++) {
n[i] = i + 100;
}
// Output each array element's value using foreach
foreach (int element in n) {
Console.WriteLine("Element value = " + element);
}
}
}
The output of the above code is −
Element value = 100 Element value = 101 Element value = 102 Element value = 103 Element value = 104 Element value = 105 Element value = 106 Element value = 107 Element value = 108 Element value = 109
Accessing Both Index and Value
When you need both the index and value, you can combine foreach with Array.IndexOf() or use a counter variable −
Example
using System;
class Program {
static void Main() {
string[] fruits = {"Apple", "Banana", "Orange", "Mango"};
int index = 0;
foreach (string fruit in fruits) {
Console.WriteLine("Index[{0}] = {1}", index, fruit);
index++;
}
}
}
The output of the above code is −
Index[0] = Apple Index[1] = Banana Index[2] = Orange Index[3] = Mango
foreach vs for Loop
| foreach Loop | for Loop |
|---|---|
| Automatically iterates through all elements | Requires manual index management |
| Read-only access to elements | Can modify elements by index |
| Cleaner syntax for simple iteration | More control over iteration process |
| Cannot access element index directly | Index is readily available |
Using foreach with Different Array Types
Example
using System;
class ArrayDemo {
static void Main() {
// Integer array
int[] numbers = {10, 20, 30, 40, 50};
Console.WriteLine("Integer array:");
foreach (int num in numbers) {
Console.Write(num + " ");
}
Console.WriteLine("<br>");
// String array
string[] names = {"John", "Alice", "Bob"};
Console.WriteLine("String array:");
foreach (string name in names) {
Console.WriteLine("Hello, " + name);
}
}
}
The output of the above code is −
Integer array: 10 20 30 40 50 String array: Hello, John Hello, Alice Hello, Bob
Conclusion
The foreach statement in C# provides a clean and simple way to iterate through array elements without managing index variables. It's ideal for read-only operations where you need to process each element sequentially, making code more readable and less error-prone than traditional for loops.
