C# How to check if we've 'had' values from a method without having to re-call the method 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 &gt; 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()) // &lt;-- 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?
I don't understand why you don't want to use a variable. However, one way is to provide an extension method for this:

public static void ForEachDo&lt;T&gt;(this IEnumerable&lt;T&gt; seq, Action&lt;T&gt; doWithEveryItem, Action doIfEmpty = null)
{
    bool isEmpty = true;
    foreach (T item in seq)
    {
        isEmpty = false;
        doWithEveryItem(item);
    }
    if (isEmpty &amp;&amp; doIfEmpty != null)
        doIfEmpty();
}


Now you can use it with everything that implements IEnumerable&lt;T&gt; like a string:

"".ForEachDo(Console.WriteLine, () =&gt; Console.WriteLine("No Events")); // prints "No Events"