Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9302521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:16:31+00:00 2026-06-18T23:16:31+00:00

Could you explain me how ‘yield’ keyword works in regards to Enumerable? For ex.

  • 0

Could you explain me how ‘yield’ keyword works in regards to Enumerable? For ex. I can’t understand how in the code below program returns an object of type IEnumerable:

class Program
    {
        static IEnumerable<T> Merge<T>(IEnumerable<T> left, IEnumerable<T> right)
            where T: IComparable<T>
        {
            IEnumerator<T> l = left.GetEnumerator();
            IEnumerator<T> r = right.GetEnumerator();

            bool l_has_data = l.MoveNext();
            bool r_has_data = r.MoveNext();

            while (l_has_data || r_has_data)
            {
                if (!l_has_data && r_has_data)
                {
                    yield return r.Current;
                    r_has_data = r.MoveNext();
                    continue;
                }
                if (!r_has_data && l_has_data)
                {
                    yield return l.Current;
                    l_has_data = l.MoveNext();
                    continue;
                }

                int comp = l.Current.CompareTo(r.Current);
                if (comp < 0)
                {
                    yield return l.Current;
                    l_has_data = l.MoveNext();
                }
                else if (comp > 0)
                {
                    yield return r.Current;
                    r_has_data = r.MoveNext();
                }
                else
                {
                    yield return l.Current;
                    yield return r.Current;
                    l_has_data = l.MoveNext();
                    r_has_data = r.MoveNext();
                }
            }
        }

If i hover over ‘.Current’ it says me that this ‘Gets the element at the current position of the enumerator’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:16:33+00:00Added an answer on June 18, 2026 at 11:16 pm

    What MSDN days :

    The yield keyword signals to the compiler that the method in which it
    appears is an iterator block. The compiler generates a class to
    implement the behavior that is expressed in the iterator block. In the
    iterator block, the yield keyword is used together with the return
    keyword to provide a value to the enumerator object. This is the value
    that is returned, for example, in each loop of a foreach statement.


    In simple word :

    yield return return a Collection of Object instead of returning a single object

        static void Main(string[] args)
        {
    
            int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
            // supposed u need to find all the numbers which are greater then 5
            // in general it could have been done like
    
            foreach (int number in numbers)
            {
                if (number > 5)
                {
                    Console.WriteLine(number);
                }
    
            }
    
            // what if u needed the numbers that are greater then 5 multiple times, each time you would have to start a loop
            // yield return helps to return a collection of int
            var needed_numbers = NeededNumbers(numbers);
    
            foreach (int neededNumber in needed_numbers)
            {
                Console.WriteLine(neededNumber);
            }
        }
    
        private static IEnumerable<int> NeededNumbers(int[] nums)
        {
            foreach (int number in nums)
            {
                if (number > 5)
                {
                    yield return number;
                }
    
            }
        }
    

    Quote from DotNetPerls

    The yield return statement is semantically equivalent to a return
    statement (which passes control flow to the calling method), followed
    by a “goto” to the yield statement in the next iteration of the
    foreach loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if someone could explain how the following code works: public interface
I was hoping someone could explain what is happening i the following code taken
I am wondering if someone could explain how I can utilise tags in an
I would be very grateful if someone could explain to me why this works:
I was wondering if somebody could explain me how pointers and string parsing works.
Could somebody please explain to me why I am receiving the below error, I
Please can someone could explain why I get this error and what to do
I am curious about the following code, somebody could explain why does it call
I was hoping someone could explain to me why i can't save new record?
I was hoping someone could explain to me why i can't save new record?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.