Currently I'm saving the values, gotten from a method, to a variable and using them later on.
This is the example code of how I do it:
var events = class.GetEvents();
if(events.Length > 0)
{
foreach(var event in events)
{
// do something
}
}
else
{
// No events
Console.Log("No Events");
}
But actually, What I'd like to achieve is something more like this:
foreach(var event in class.GetEvents()) // <-- If none, I'd like to know.
{
// do something
}
However, I still need to log that there weren't any results. I'd have to do a check, and re-call the method to see if there are any results.. OR i++ in my loop, and if that's a 0 => log.
Is there a simpler way to do this?
ForEach
method and call it with the loop statement and an else statement like this:class.GetEvents().ForEach((event) => { /* loop block */ }, () => { /* else statement */ });
\$\endgroup\$ – xander Nov 30 '17 at 14:55