C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
In C#, both for
and foreach
loops are used to iterate over a collection of items. The main difference between them is that for
loops use an index variable to iterate over a collection, while foreach
loops automatically iterate over each element in a collection without the need for an index. In this tutorial, we'll cover how to use for
and foreach
loops in C#.
for
loop with arrays:Here's an example of using a for
loop to iterate over an array of integers:
int[] numbers = { 1, 2, 3, 4, 5 }; for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); }
The for
loop uses the index variable i
to access each element in the numbers
array.
foreach
loop with arrays:Here's the same example using a foreach
loop:
int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { Console.WriteLine(number); }
The foreach
loop iterates through each element in the numbers
array, assigning the current element to the number
variable.
for
loop with lists:You can use the for
loop with collections like List<T>
. Here's an example using a List<string>
:
using System; using System.Collections.Generic; List<string> names = new List<string>() { "Alice", "Bob", "Charlie" }; for (int i = 0; i < names.Count; i++) { Console.WriteLine(names[i]); }
In this example, the for
loop uses the index variable i
to access each element in the names
list.
foreach
loop with lists:Here's the same example using a foreach
loop:
using System; using System.Collections.Generic; List<string> names = new List<string>() { "Alice", "Bob", "Charlie" }; foreach (string name in names) { Console.WriteLine(name); }
In this example, the foreach
loop iterates through each element in the names
list, assigning the current element to the name
variable.
for
vs. foreach
:for
loop when you need to access elements by their index or when you need to modify the elements of the collection.foreach
loop when you only need to read the elements of the collection or when you want a more elegant and concise way of iterating over a collection.In this tutorial, we've covered how to use both for
and foreach
loops in C#. The for
loop is useful when you need to access elements by their index or modify the elements, while the foreach
loop provides a clean and elegant way to iterate over a collection without the need for an index variable.
How to use for loop in C#:
for
loop in C# provides a concise and flexible way to iterate over a range of values.using System; class Program { static void Main() { // Example of for loop for (int i = 0; i < 5; i++) { Console.WriteLine(i); } } }
C# for loop examples:
for
loop can be used in various scenarios, such as iterating over arrays, lists, or a range of values.using System; class Program { static void Main() { // Example of for loop with an array int[] numbers = { 1, 2, 3, 4, 5 }; for (int i = 0; i < numbers.Length; i++) { Console.WriteLine(numbers[i]); } } }
C# nested for loop example:
for
loops can be nested to traverse elements in a two-dimensional array or perform other nested iterations.using System; class Program { static void Main() { // Example of nested for loop for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Console.Write($"{i}-{j} "); } Console.WriteLine(); } } }
C# for loop through a range of numbers:
for
loop can easily iterate through a specified range of numbers.using System; class Program { static void Main() { // Example of for loop through a range for (int i = 1; i <= 5; i++) { Console.WriteLine(i); } } }
C# foreach loop with break and continue:
foreach
loop can incorporate break
and continue
statements to control the flow of execution.using System; class Program { static void Main() { int[] numbers = { 1, 2, 3, 4, 5 }; // Example of foreach loop with break and continue foreach (int number in numbers) { if (number == 3) break; // exit the loop when 3 is encountered if (number % 2 == 0) continue; // skip even numbers Console.WriteLine(number); } } }
Looping over strings with for loop in C#:
for
loops are suitable for iterating over characters in a string.using System; class Program { static void Main() { string text = "C#"; // Example of for loop with a string for (int i = 0; i < text.Length; i++) { Console.WriteLine(text[i]); } } }