Syntax

C# Foreach Loop Syntax

In C#, foreach loop: loops over every item — foreach (var item in items) { ... }

Quick Answer

Syntax
foreach (var item in items) { ... }
C#Syntax

Foreach Loop

Code example
foreach (var item in items) { ... }

Loops over every item.

Learn More

How to foreach loop in C#

  1. Open your C# file or editor.
  2. Copy the example, then replace its sample names and values for your code.

C# Foreach Loop Syntax

Use foreach (var item in items) { ... }.

Related Syntax References

Frequently asked questions

What is the C# syntax for foreach loop?

Use this pattern: foreach (var item in items) { ... }

Can I copy this C# code example?

Yes. Use Copy code and adapt the example to your program.

Is this C# syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify