
- 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
How to iterate over a C# list?
Declare a list and add elements −
var products = new List < string > (); // adding elements products.Add("Belts"); products.Add("T-Shirt"); products.Add("Trousers");
Use a loop to iterate −
foreach(var p in products) { Console.WriteLine(p); }
Example
Let us see the complete example −
using System; using System.Collections.Generic; public class Demo { public static void Main(string[] args) { var products = new List < string > (); products.Add("Belts"); products.Add("T-Shirt"); products.Add("Trousers"); Console.WriteLine("Our list...."); foreach(var p in products) { Console.WriteLine(p); } } }
- Related Articles
- How to iterate over a Java list?
- How to iterate over a list in Java?
- Iterate over a list in Python
- How to iterate over a list of files with spaces in Linux?
- Program to iterate over a List using Java 8 Lambda
- How to iterate over a C# dictionary?
- How to iterate over a C# tuple?
- How to iterate over a Hashmap in Kotlin?
- How to iterate a Java List?
- How to iterate over all MongoDB databases?
- Java Program to Iterate over a HashMap
- Java Program to Iterate over a Set
- C++ Program to Iterate Over a Dictionary
- Golang program to iterate over a Slice
- How to iterate over rows in a DataFrame in Pandas?

Advertisements