What are IEnumerator and Coroutine?

Just a few quick question?

What are IEnumerator and Coroutine?

What do they do?

How should they be used?

And where is the best place for them to be used?

Example code would be highly appreciated.

I tried looking online but I couldn’t find anything that gives me a basic and simple explanation.

Many Thanks in advance!!

They are both types representing an object.

Coroutine is a Unity engine class while IEnumerator belongs to the .NET.

When you iterate through a collection or access a large file, waiting for the whole action would stop all others, IEnumerator allows to stop the process at a specific moment, return that part of object (or nothing) and gets back to that point whenever you need it.This until the MoveNext method from IEnumerator returns false indicating we reached the end of the collection/file.

Obviously, it is up to the programmer to implement IEnumerator so that your collection is cut properly, there is no magic. If you make a class a IEnumerator, there will be a bunch of methods to implement so the iteration goes accordingly. For most built-in classes it is already done so you can use it the process without doing anything. For instance:

 foreach (var item in collection){}

This can only work if the collection is a type inheriting from IEnumerator. All arrays, lists and so on do that.

The coroutine is the object within unity that allows to start parallel action (or almost). When using StartCoroutine, unity creates a new object of type Coroutine, this object performs some action and then returns a IEnumerator object (or nothing):

IEnumerator MyCoroutine(){
    while(true){yield return null;}
}

It returns a null object or a IEnumerator. A coroutine needs a MonoBehaviour to be attached to so it will use it as a parent process (if familiar with Unix). That parent process checks on each frame if it has a coroutine pending, it does at various time in the frame since you can control when you want the coroutine to get back on:

IEnumerator MyCoroutine(){
    int index = 0;
    while(true){
        yield return new WaitForEndOfFrame(); 
        Debug.Log("Frame is over");
        if(++index == 3){yield break;}
    }
}

This one above, will start from code, then run until it hits the return line. At that point, we are in the Update of a MonoBehaviour (or Late/Fixed/Start/…), and it gets back where it stops. At the end of the frame, Unity will look into that MonoBehaviour and find that coroutine, returns from the yield return line and continues. It will do that 3 times until break. On each round it retuns a IEnumerator object from the WaitForEndOFrame method.

Conclusion: IEnumerator is a .NET type that is used to fragment large collection or files, or simply to pause an iteration. Coroutine is a Unity type that is used to create parallel actions returning a IEnumerator to do so.

Coroutines are ways to write code that says “wait at this line for a little.”

To be more precise, WaitForSeconds and “wait until the next frame” (yield return null) are the commands that make code wait at one spot. They can only be used inside Coroutines.

See the docs for examples: Unity Script Reference – Overview: Coroutines & Yield

A coroutine is a function written with the word IEnumerator in front, just because. You can ignore what IEnumerator is – it just has to go there. It’s really a different C# trick, which is being McGuyvered by Unity, like using a paperclip to pick a lock.