C# Keywords Tutorial Part 101: yield

C# Keywords Tutorial Part 101: yield

C# is a powerful programming language that offers a range of features and functionalities to help developers create efficient and high-performing applications. One such feature is the “yield” keyword, which is used to create iterator blocks in C#. In this blog post, we’ll take a closer look at the “yield” keyword and explore how it can be used to create efficient and easy-to-use iterators in your C# applications.

The “yield” Keyword in C#

A strong feature of C# is the “yield” keyword, which enables programmers to build iterator blocks that may be used to loop around a group of objects. A function or property that returns an iterator, which can be used to iterate through a list of objects, is known as an iterator block.

utilize the “yield” keyword in the function or property you wish to utilize as an iterator in C# to build an iterator block. The “yield” keyword tells the compiler that a method or property is an iterator and should return an iterator object when it appears in your code.

Code Examples

To better understand how the “yield” keyword works in C#, let’s take a look at some code examples. In the following examples, we’ll use the “yield” keyword to create iterator blocks that can be used to iterate over a collection of integers.

Example 1:

public static IEnumerable<int> GetNumbers(int count)
{
    for (int i = 1; i <= count; i++)
    {
        yield return i;
    }
}

In this example, we’ve created an iterator block called “GetNumbers” that takes an integer value “count” as a parameter. Inside the method, we use a “for” loop to generate a sequence of integers from 1 to “count”. When the “yield” keyword is encountered in the loop, it returns the current value of “i” to the iterator.

To use this iterator block, you can simply call it from your code and use a “foreach” loop to iterate over the sequence of integers that it generates. Here’s an example:

foreach (int number in GetNumbers(5))
{
    Console.WriteLine(number);
}

This code will output the following sequence of integers:

1
2
3
4
5

Example 2:

public static IEnumerable<int> GetEvenNumbers(int count)
{
    int i = 0;
    while (i < count)
    {
        i += 2;
        yield return i;
    }
}

In this example, we’ve created an iterator block called “GetEvenNumbers” that takes an integer value “count” as a parameter. Inside the method, we use a “while” loop to generate a sequence of even integers. When the “yield” keyword is encountered in the loop, it returns the current value of “i” to the iterator.

To use this iterator block, you can simply call it from your code and use a “foreach” loop to iterate over the sequence of even integers that it generates. Here’s an example:

foreach (int number in GetEvenNumbers(5))
{
    Console.WriteLine(number);
}

This code will output the following sequence of even integers:

2
4
6

Share this post

Comment (1)

  • Sophia William Reply

    This post is a must-read for anyone interested in the topic. Thank you for sharing your knowledge and insights.

    May 2, 2023 at 4:52 AM

Leave a Reply

Your email address will not be published. Required fields are marked *