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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s