LANGUAGE

foreach loop

Enumerates elements without manual indexing; ordinary value iteration variables cannot be reassigned.

Formal Syntax & Structure

csharp syntaxcsharp
foreach (Type item in collection) { ... }

Working Example

Example Snippetcsharp
int[] stock = { 1, 2 }; foreach (int n in stock) { System.Console.WriteLine(n); }
C# & .NET Reference