Is there a better (more concise) way of iterating over the same collections objects in an if/else condition than the following:
bool condition = DetermineConditionValue();
if(condition)
{
    foreach(var v in variables)
    {
        PerformAction(v);
    }
else
{
    foreach(var v in variables)
    {
        PerformAnotherAction(v);
    }
}
Is there a better way to avoid writing the loop twice?
You could use Action<T>
Action<YourParameterTypeHere> actionToDo = DetermineConditionValue()
      ? PerformAction 
      : PerformAnotherAction;
foreach(var v in variables)
{
    actionToDo(v);
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With