Hi, my self Ravi Vishwakarma. I have completed my studies at SPICBB Varanasi. now I completed MCA with 76% form Veer Bahadur Singh Purvanchal University Jaunpur.
SWE @ MindStick | Software Engineer | Web Developer | .Net Developer | Web Developer | Backend Engineer | .NET Core Developer
The foreach loop in C# is used to iterate over a collection of items, such as
arrays, lists, or other enumerable
collections. It is beneficial for iterating through elements without knowing the
collection size or managing loop counters. The foreach loop provides a cleaner and more readable way to traverse collections than the traditional for loop.
Here is the basic syntax of a foreach loop in C#:
foreach (var item in collection)
{
// Code to execute for each item
}
Example Usage
Iterating Over an Array:
int[] numbers = { 1, 2, 3, 4, 5 };
foreach (int number in numbers)
{
Console.WriteLine(number);
}
Iterating Over a List:
List<string> names = new List<string> { "Alice", "Bob", "Charlie" };
foreach (string name in names)
{
Console.WriteLine(name);
}
Key Points
The foreach loop can only be used with collections that implement the
IEnumerable interface.
The loop variable (e.g., item in the examples) is read-only within the scope of the loop. You cannot assign a new value to
item within the loop body.
The foreach loop automatically handles the iteration bounds, so you do not need to manage index variables manually.
It is generally safer and less error-prone than using a for loop with index variables, especially when working with collections that can change in size or when the iteration logic is complex.
Limitations
You cannot modify the collection itself (e.g., adding or
removing elements) while iterating over it using a foreach loop. Doing so will result in an
InvalidOperationException.
If you need to modify elements of a collection, consider using a for loop or other appropriate methods that provide access to the collection by index.
Liked By
Write Answer
Foreach Loop in C#
Join MindStick Community
You have need login or register for voting of answers or question.
Ravi Vishwakarma
09-Jun-2024The foreach loop in C# is used to iterate over a collection of items, such as arrays, lists, or other enumerable collections. It is beneficial for iterating through elements without knowing the collection size or managing loop counters. The foreach loop provides a cleaner and more readable way to traverse collections than the traditional for loop.
Here is the basic syntax of a foreach loop in C#:
Example Usage
Iterating Over an Array:
Iterating Over a List:
Key Points
foreach
loop can only be used with collections that implement theIEnumerable
interface.item
in the examples) is read-only within the scope of the loop. You cannot assign a new value toitem
within the loop body.foreach
loop automatically handles the iteration bounds, so you do not need to manage index variables manually.for
loop with index variables, especially when working with collections that can change in size or when the iteration logic is complex.Limitations
foreach
loop. Doing so will result in anInvalidOperationException
.for
loop or other appropriate methods that provide access to the collection by index.