If you have played around in C# enough, you will have come across this mystifying keyword, ‘yield’. Here I will try to explain in a nutshell what it does.

Say that you have a function like this:

void SomeFunction(){
    //code A...

    yield return something;

    //code B...
}

To understand what yield does, I will run you through the execution of the function. First, code A is executed. Then, SomeFunction is paused until something iterates over the next value. If something is a list, it will move to the next item and execution of SomeFunction will resume. If something is a function, it will get executed and when it finishes, SomeFunction will resume.

In a nutshell, this is how ‘yield’ works.