
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
foreach Loop in C#
The foreach loop executes a statement or a block of statements for each element in an instance of the type that implements the System.Collections.IEnumerable or System.Collections.Generic.IEnumerable<T> interface.
Example
Let us see an example of foreach loop −
using System; using System.Collections.Generic; public class Demo { public static void Main(){ LinkedList<int> linkedList = new LinkedList<int>(); linkedList.AddLast(25); linkedList.AddLast(50); linkedList.AddLast(100); linkedList.AddLast(200); linkedList.AddLast(400); linkedList.AddLast(500); linkedList.AddLast(550); linkedList.AddLast(600); linkedList.AddLast(800); linkedList.AddLast(1200); Console.WriteLine("Count of nodes = " + linkedList.Count); foreach(int val in linkedList){ Console.WriteLine(val); } Console.WriteLine("Does the LinkedList has node 800? "+linkedList.Contains(800)); } }
Output
This will produce the following output −
Count of nodes = 10 25 50 100 200 400 500 550 600 800 1200 Does the LinkedList has node 800? True
- Related Articles
- PHP foreach Loop.
- Using foreach loop in arrays in C#
- Multiple index variables in PHP foreach loop
- Iterating C# StringBuilder in a foreach loop
- How does the Java “foreach” loop work?
- How to use PowerShell break statement in foreach loop?
- Stripping last comma from a foreach loop in PHP?
- PHP Casting Variable as Object type in foreach Loop
- The internal working of the ‘foreach’ loop in PHP
- How to use PSCustomObject in PowerShell foreach parallel loop?
- How to use the foreach loop parallelly in PowerShell?
- How to show a foreach loop using a flow chart in JavaScript?’
- How do you use ‘foreach’ loop for iterating over an array in C#?
- Implement MongoDB toLowerCase() in a forEach loop to update the name of students?
- How to get the current index of an array while using forEach loop in Kotlin?

Advertisements