Occasionally, you may need to iterate over collections and evaluate various conditions for each element. Often, you need to check multiple conditions for each item, whether those conditions involve the same or different data types. Below are a couple of approaches for applying multiple conditions in a ForEach loop, using logical operators, type checks, and custom classes.
Multiple Variable Value Checking
You can use a single line of code to check multiple conditions within a ForEach loop using logical operators. This is a simple and effective way to ensure each item meets specific criteria. Below is an example that demonstrates how to iterate over a list of integers and check two conditions at once: whether a number is even and greater than 5.
Example Implementation
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
numbers.ForEach(n =>
{
if (n % 2 == 0 && n > 5) // Check if the number is even and greater than 5
{
Console.WriteLine(n);
}
});
In this code, the lambda expression n => { if (n % 2 == 0 && n > 5) { Console.WriteLine(n); } } checks if each number is even and greater than 5 in one concise line. This technique allows you to apply multiple conditions efficiently within the ForEach method.
Using Object Type with Type Checking
When working with collections that contain elements of different types, you can use type checking to perform multiple condition checks. This approach involves using the is keyword for pattern matching, ensuring that the conditions are checked only for elements of the correct type.
Example Implementation
List<object> mixedList = new List<object> { 1, "hello", 3.14, 42, "world" };
mixedList.ForEach(item =>
{
if (item is int intValue && intValue > 10) // check if item is an integer and greater than 10
{
Console.WriteLine($"Integer: {intValue}");
}
else if (item is string strValue && strValue.Length > 4) // check if item is a string and length is greater than 4
{
Console.WriteLine($"String: {strValue}");
}
else if (item is double doubleValue && doubleValue < 5.0) // check if item is a double and less than 5.0
{
Console.WriteLine($"Double: {doubleValue}");
}
});
In this code, the mixedList contains elements of different types, such as integers, strings, and doubles. By using type checking (is keyword), the program first checks the type of each element, then applies the appropriate conditions based on the type. This allows you to handle multiple types and check different conditions for each type.
Using a Custom Class with Multiple Properties
For custom classes that have multiple properties, you can check conditions for each property within a ForEach loop. This is useful when you want to evaluate specific properties of objects in a collection. Below is an example where we have a Person class with properties Age and Height, and we check if both conditions are met for each person in the list.
Example Implementation
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public double Height { get; set; }
}
List<Person> people = new List<Person>
{
new Person { Name = "Alice", Age = 30, Height = 5.5 },
new Person { Name = "Bob", Age = 15, Height = 6.0 },
new Person { Name = "Charlie", Age = 40, Height = 5.9 }
};
people.ForEach(person =>
{
if (person.Age > 25 && person.Height < 6.0) // check if age is greater than 25 and height is less than 6.0
{
Console.WriteLine($"{person.Name} meets the criteria.");
}
});
In this code, I check if the Age is greater than 25 and the Height is less than 6.0 for each Person object in the list. The ForEach method provides an elegant way to apply these checks to each object in the collection.
General Approach
When working with different types in a ForEach loop, there are several best practices to follow:
Use Type Checking: Check the type of the object using is, as, or type conversion. This ensures that the conditions you apply are type-safe.
Apply Type-Specific Logic: Once the type is confirmed, apply the appropriate conditions for that type.
Handle Casting and Conversions: If needed, use casting or conversion to perform operations specific to the type.
Conclusion
Exploring different methods to efficiently check multiple variable values in a ForEach loop using logical operators, type checking, and handling custom classes with multiple properties. These techniques can simplify the process of iterating over collections and applying complex conditions. It also enhances code readability and maintainability, especially when dealing with collections of mixed types or custom objects.
Top comments (0)