C# yield keyword (With Examples) (2024)

The yield keyword performs custom iteration over a collection like list, array, etc.

Two Forms of yield Keyword

The yield keyword is used in two forms:

  • yield return - returns an expression at each iteration
  • yield break - terminates the iteration

yield return in an Iterator

The yield keyword is used in an iterator as:

yield return <expression>; 

We use yield return inside an iterator as:

using System;using System.Collections.Generic;class Program{ // define an iterator method static IEnumerable<int> getNumber() { // create a list of integers List<int> myList = new List<int> { -1, -4, 3, 5 }; foreach (var num in myList) { // returns positive number from myList 

if (num >= 0) { yield return num; // location of the code is preserved // so on the next iteration getNumber() is executed from here Console.WriteLine("...."); }

} } static void Main() { // display return values of getNumber() foreach (var items in getNumber()) { Console.WriteLine(items); } }}

Output

3....5....

In the above example, we have created an iterator method getNumber() inside which we have used yield return.

Here, when getNumber() is called from foreach of Main(), the code inside getNumber() is executed until it reaches yield return.

When it reaches yield return, the current location of the code is preserved and the control goes back to the caller(i.e. foreach loop) and 3 is printed.

In the next iteration of foreach, the getNumber() method is called again. This time, getNumber() is executed from the preserved location of the code. This means,

Console.WriteLine("....");

gets executed and getNumber() resumes until it encounters another yield return and the same process continues till iterating over myList is completed.

Let's discuss the working of the above program with the help of an image.

Working of yield return

C# yield keyword (With Examples) (1)

1. foreach calls getNumber() - At first, the code inside the Main() function gets executed. Inside Main(), we have created a foreach loop that calls getNumber() method.

2. Inside getNumber(), myList is created.

3. Then, the foreach loop inside getNumber() starts iterating over myList. Notice the code,

if (num >= 0){ yield return num; // location of the code is preserved // so on the next iteration getNumber() is executed from here Console.WriteLine("....");}

When num = 3, num >= 0 returns True and yield return is encountered. Two operations occur:

  • pauses getNumber() and preserves the current location of the code
  • control goes back to the foreach loop (caller)

4. Control goes back to the caller - Here, the returned value 3 is printed and the next iteration of foreach begins.

In the next iteration, the getNumber() method is called again. In this call, getNumber() is resumed from the preserved location.

This means Console.WriteLine("...."); gets executed.

Now, in if block, num = 5 so num >= 0 is True. And again we encounter yield return.

5. Again, control goes back to the caller (inside foreach). Here, 5 is printed and foreach calls getNumber() in the next iteration.

The method resumes from the preserved location and prints ..... Since there are no other elements left in myList, iteration is stopped.

Frequently Asked Question

Why use yield keyword?

We have to create a temporary collection if we do not use yield return. For example,

using System;using System.Collections.Generic;class Program{ // define an iterator method static IEnumerable<int> getEven() { // create a list of integers List<int> myList = new List<int>() { 1, 2, 3, 4, 5 };

// create an empty temporary list List<int> tempList = new List<int>();

// iterate through myList foreach (var i in myList) { if (i % 2 == 0) { // adds i to temporary list tempList.Add(i); } } return tempList; } static void Main() { // display return values of getEven() foreach (var items in getEven()) { Console.WriteLine(items); } }}

Output

24

Here, we have created a temporary list tempList that stores the even numbers present inside myList.

Creating a temporary list slows down the computation and requires more memory when we are executing a large number of values.

yield break

The yield break ends an iterator ( list, array, etc) block. Let's take an example to understand it clearly.

using System;using System.Collections.Generic;class Program{ // define an iterator method static IEnumerable<string> getString() { // create a list of strings List<string> myList = new List<string> { "Sun", "Mon", "Tue" }; foreach (var day in myList) { if (day == "Mon") {

// terminates the iterator block after encountering "Mon" yield break;

} yield return day; } } static void Main() { // display return values of getString() foreach (var items in getString()) { Console.WriteLine(items); } }}

Output

Sun

Here inside the getString() method, notice

foreach (var day in myList){

if (day == "Mon") { // terminates the iterator block after encountering "Mon" yield break; }

yield return day;}

Here,

  • "Sun"=="Mon" is False so if block is not executed. Hence, the program executes yield return day and returns "Sun" to getString().
  • "Mon" == "Mon is True so if block is executed. Here, yield break is encountered and it ends the iterator block returning nothing.

Basically, if yield break is encountered, it indicates there are no more elements left in the collection.

Note: The yield break is different from break statement because break statement terminates the closest loop in normal method and yield break terminates the iterator method and transfers the control of the program to the caller.

yield break works just like return statement that returns nothing.

C# yield keyword (With Examples) (2024)

References

Top Articles
Gluten Free Dinner Rolls Recipe SUPER SOFT and Easy! - Life After Wheat
Homemade Stuffing Recipe | The Bewitchin' Kitchen
Ffxiv Act Plugin
Ohio Houses With Land for Sale - 1,591 Properties
Main Moon Ilion Menu
Ffxiv Shelfeye Reaver
Trabestis En Beaumont
Frank Lloyd Wright, born 150 years ago, still fascinates
What happened to Lori Petty? What is she doing today? Wiki
2024 Fantasy Baseball: Week 10 trade values chart and rest-of-season rankings for H2H and Rotisserie leagues
877-668-5260 | 18776685260 - Robocaller Warning!
Stl Craiglist
Pickswise the Free Sports Handicapping Service 2023
Craigslist Nj North Cars By Owner
Barstool Sports Gif
AB Solutions Portal | Login
Tcu Jaggaer
What Was D-Day Weegy
W303 Tarkov
More Apt To Complain Crossword
Fredericksburg Free Lance Star Obituaries
The Murdoch succession drama kicks off this week. Here's everything you need to know
Fairy Liquid Near Me
Mail.zsthost Change Password
Tamilrockers Movies 2023 Download
Msu 247 Football
Forum Phun Extra
Foxy Brown 2025
Christina Steele And Nathaniel Hadley Novel
Hobby Stores Near Me Now
What Is The Lineup For Nascar Race Today
Craigslist Illinois Springfield
Sadie Sink Reveals She Struggles With Imposter Syndrome
Danielle Ranslow Obituary
The Collective - Upscale Downtown Milwaukee Hair Salon
Summoners War Update Notes
Craigs List Jax Fl
Life Insurance Policies | New York Life
Trebuchet Gizmo Answer Key
Shuaiby Kill Twitter
Great Clips Virginia Center Commons
Devon Lannigan Obituary
Walmart Car Service Near Me
FREE - Divitarot.com - Tarot Denis Lapierre - Free divinatory tarot - Your divinatory tarot - Your future according to the cards! - Official website of Denis Lapierre - LIVE TAROT - Online Free Tarot cards reading - TAROT - Your free online latin tarot re
Is Ameriprise A Pyramid Scheme
Blue Beetle Showtimes Near Regal Evergreen Parkway & Rpx
Mybiglots Net Associates
Enr 2100
Sherwin Source Intranet
Nope 123Movies Full
Myra's Floral Princeton Wv
Every Type of Sentinel in the Marvel Universe
Latest Posts
Article information

Author: Aracelis Kilback

Last Updated:

Views: 6451

Rating: 4.3 / 5 (44 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aracelis Kilback

Birthday: 1994-11-22

Address: Apt. 895 30151 Green Plain, Lake Mariela, RI 98141

Phone: +5992291857476

Job: Legal Officer

Hobby: LARPing, role-playing games, Slacklining, Reading, Inline skating, Brazilian jiu-jitsu, Dance

Introduction: My name is Aracelis Kilback, I am a nice, gentle, agreeable, joyous, attractive, combative, gifted person who loves writing and wants to share my knowledge and understanding with you.