C# foreach loop with conditions

C# foreach loop with conditions

In C#, you can use the foreach loop in combination with conditions (if statements) to filter or apply specific actions to elements during iteration. The foreach loop iterates over a collection, such as an array or a collection implementing IEnumerable<T>, and you can use conditional statements within the loop to control the flow of the iteration.

Here's an example of using a foreach loop with conditions:

using System; using System.Collections.Generic; public class Program { public static void Main() { List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // Example 1: Filtering elements using if statement Console.WriteLine("Even numbers:"); foreach (int num in numbers) { if (num % 2 == 0) { Console.Write(num + " "); } } // Example 2: Applying an action based on a condition Console.WriteLine("\n\nNumbers less than 5:"); foreach (int num in numbers) { if (num < 5) { Console.Write(num + " "); } } } } 

In this example, we have a list of integers named numbers. We use two different foreach loops with conditions:

  1. Example 1: Filtering even numbers.

    • We use an if statement to filter out even numbers from the list and print them.
  2. Example 2: Numbers less than 5.

    • We use an if statement to check if each number in the list is less than 5 and print those numbers.

The output of the program will be:

Even numbers: 2 4 6 8 10 Numbers less than 5: 1 2 3 4 

By using conditional statements inside the foreach loop, you can control which elements to process, skip certain elements, or perform specific actions on selected elements during the iteration.

Examples

  1. "C# foreach loop with condition for arrays"

    // Code to demonstrate foreach loop with condition for arrays int[] numbers = { 1, 2, 3, 4, 5 }; foreach (int number in numbers) { if (number % 2 == 0) { // Your code for each even number } } 

    Description: This code uses a simple if condition within the foreach loop to execute code only for elements that meet a specific condition.

  2. "C# foreach loop with condition for List<T>"

    // Code to demonstrate foreach loop with condition for List<T> List<string> names = new List<string> { "Alice", "Bob", "Charlie", "David" }; foreach (string name in names) { if (name.Length > 5) { // Your code for each name with length greater than 5 } } 

    Description: This code uses an if condition within the foreach loop to execute code only for elements in a List<T> that meet a specific condition.

  3. "C# foreach loop with condition for IEnumerable<T>"

    // Code to demonstrate foreach loop with condition for IEnumerable<T> IEnumerable<int> sequence = Enumerable.Range(1, 10); foreach (int number in sequence) { if (number % 3 == 0) { // Your code for each number divisible by 3 } } 

    Description: This code uses an if condition within the foreach loop to execute code only for elements in an IEnumerable<T> that meet a specific condition.

  4. "C# foreach loop with multiple conditions for Dictionary<TKey, TValue>"

    // Code to demonstrate foreach loop with multiple conditions for Dictionary<TKey, TValue> Dictionary<string, int> keyValuePairs = new Dictionary<string, int> { { "One", 1 }, { "Two", 2 }, { "Three", 3 } }; foreach (var pair in keyValuePairs) { if (pair.Value > 1 && pair.Key.Length > 3) { // Your code for each key-value pair meeting multiple conditions } } 

    Description: This code uses an if condition within the foreach loop to execute code only for key-value pairs in a Dictionary<TKey, TValue> that meet multiple conditions.

  5. "C# foreach loop with condition for HashSet<T>"

    // Code to demonstrate foreach loop with condition for HashSet<T> HashSet<int> uniqueNumbers = new HashSet<int> { 2, 4, 6, 8, 10 }; foreach (int number in uniqueNumbers) { if (number % 3 == 0) { // Your code for each element divisible by 3 } } 

    Description: This code uses an if condition within the foreach loop to execute code only for elements in a HashSet<T> that meet a specific condition.

  6. "C# foreach loop with condition for Queue<T>"

    // Code to demonstrate foreach loop with condition for Queue<T> Queue<double> queue = new Queue<double>(new[] { 1.1, 2.2, 3.3, 4.4 }); foreach (double value in queue) { if (value > 2.0) { // Your code for each element greater than 2.0 } } 

    Description: This code uses an if condition within the foreach loop to execute code only for elements in a Queue<T> that meet a specific condition.

  7. "C# foreach loop with condition for Stack<T>"

    // Code to demonstrate foreach loop with condition for Stack<T> Stack<string> stack = new Stack<string>(new[] { "apple", "banana", "orange" }); foreach (string fruit in stack) { if (fruit.Length > 5) { // Your code for each fruit with length greater than 5 } } 

    Description: This code uses an if condition within the foreach loop to execute code only for elements in a Stack<T> that meet a specific condition.

  8. "C# foreach loop with condition for ConcurrentBag<T>"

    // Code to demonstrate foreach loop with condition for ConcurrentBag<T> ConcurrentBag<int> concurrentBag = new ConcurrentBag<int> { 10, 20, 30, 40, 50 }; foreach (int value in concurrentBag) { if (value % 20 == 0) { // Your code for each element divisible by 20 } } 

    Description: This code uses an if condition within the foreach loop to execute code only for elements in a ConcurrentBag<T> that meet a specific condition.

  9. "C# foreach loop with condition for LinkedList<T>"

    // Code to demonstrate foreach loop with condition for LinkedList<T> LinkedList<int> linkedList = new LinkedList<int>(new[] { 10, 15, 20, 25, 30 }); foreach (int value in linkedList) { if (value % 2 == 0) { // Your code for each even value } } 

    Description: This code uses an if condition within the foreach loop to execute code only for elements in a LinkedList<T> that meet a specific condition.

  10. "C# foreach loop with condition for a custom class"

    // Code to demonstrate foreach loop with condition for a custom class List<Person> people = GetPeople(); foreach (Person person in people) { if (person.Age >= 18 && person.City == "New York") { // Your code for each adult person living in New York } } 

    Description: This code uses an if condition within the foreach loop to execute code only for elements of a custom class (Person) that meet a specific condition.


More Tags

openxml byte transfer cell-formatting null-check shared-hosting event-bubbling android-networking technical-indicator file-rename

More C# Questions

More Biochemistry Calculators

More Animal pregnancy Calculators

More Bio laboratory Calculators

More Tax and Salary Calculators