IEnumerators saving passed value into two different variables.

I’m still fighting windmills trying to create ReplayCoroutine method for my CoroutineManager. My question is mostly based for IEnumerators:

If I pass and IEnumerator in a method

CoroutineNode ReplayCoroutine(CoroutineNode cn)

and have a custom class

public class CoroutineNode
{
   public fiber;
   public fiberToCopy;


   public CoroutineNode(IEnumerator _fiber)
    {
        this.fiber = _fiber;
        this.fiberToCopy = _fiber;
    }

   public CoroutineNode(CoroutineNode cn)
   {
      fiber = cn.fiber;
      fiberToCopy = cn.fiberToCopy;
   }
}

and now when I want to copy I pass not the fiber, but fiberToCopy which then this new CoroutineNode should have IEnumerators that are not iterated yet since its a copy of the original one. Is value of IEnumerator passed by ref or value?

I’ve read a bunch of stuff on msdn and as I get it most people tend not to fu** with IEnumerators in a way I am trying to do it. Any help is appreciated!

An Interface is a Reference Type and is therefore passed by reference. In your code finer and fiberToCopy will refer to the same object.

You should pass an IEnumerable (a minimal enumerable interface to the underlying collection) then spawn a new IEnumerator whenever you need.

public IEnumerable fiberSource;
public IEnumerator fiberEnumerator;

public CoroutineNode(IEnumerable fiberSource)
{
    this.fiberSource = fiberSource;
    this.fiberEnumerator = fiberSource.GetEnumerator();
}
public CoroutineNode(CoroutineNode cn) : this(cn.fiberSource)
{}

Edit: per Bunny’s comments below you can indeed use IEnumerable (rather than IEnumerator) as the Coroutine return type so you can pass it to the CoroutineNode constructor. It’s an interesting use-case… I’m glad this worked for you!