Using an IEnumerator without a foreach loop in order to simulate a state machine I am working with an object that needs to run an ExecuteEffect() method. Sometimes, the ExecuteEffect() only needs to run once, while in other cases, the ExecuteEffect() needs to keep running once / frame until some criteria is met (in my example, a real-time delay).

To solve this, I used an IEnumerator:

public override IEnumerator&lt;bool&gt; ExecuteEffect()
    {
        if(delay &gt; 0)
        {
            if(executionStart == -1m)
            {
                executionStart = GetRealTime();
                yield return false;
            }
            if(GetRealTime() &gt; executionStart + delay)
            {
                DealDamage();
                yield return true;
            }
            else
            {
                yield return false;
            }
        }
        else
        {
            DealDamage();
            yield return true;
        }
  }


Then, when I'm running ExecuteEffect(), if the IEnumerator&lt;bool&gt; returns false, it gets added to a list of objects that is continuously checked until their ExecuteEffect() method returns true.

GenericEffect ge = GetEffect();
IEnumerator&lt;bool&gt; temp = ge.ExecuteEffect();
temp.MoveNext();
if(!temp.Current)
{
    //Foo not finished, add to list of active Foo
    activeEffects.Add(ge);
}


and, elsewhere:

activeEffects.RemoveAll( s =&gt;
                         {
                               IEnumerator&lt;bool&gt; temp = s.ExecuteEffect();
                               temp.MoveNext();
                               if(temp.Current)
                               {
                                   return true;
                               }
                               else
                               {
                                  return false;
                               }
                          });


This all feels very wordy, especially the temp variable to hold the IEnumerator&lt;bool&gt; and the explicit call to GetNext(). And, of course, I have no idea if this is an utter butchering of the intended uses of IEnumerator&lt;T&gt;.

Is there a better way to get this kind of effect? Or is this just an unconventional (albeit valid) use of IEnumerator&lt;T&gt;?
You could make life slightly easier for yourself by using the iterator slightly more properly:

public override IEnumerator&lt;bool&gt; ExecuteEffect()
{
    if(delay &lt; 0 || GetRealTime() &gt; executionStart + delay)
    {
        DealDamage();
        yield break;
    }
    if(executionStart == -1m)
    {
        executionStart = GetRealTime();
    }
    yield return false;
}


Then you could actually use MoveNext 'correctly' as it will return false when you get to the end.

To be completely and brutally honest, I'd still rage erase all of this code if I found it in a project I worked on. Why is it an enumerator of booleans? None of it's particularly obvious.

As an aside:

if(temp.Current)
{
    return true;
}
else
{
    return false;
}


is 100% the same as

return temp.Current;