A question regarding yield break and yield return
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: A question regarding yield break and yield return

  1. #1
    Join Date
    Jul 2005
    Posts
    936

    A question regarding yield break and yield return

    Here is an article explaining how to use yield break and yield return, http://iserialized.com/a-quick-start...d-yield-break/
    In the article, there is a method without using yield,
    Code:
    public static List<int> GetResultList() 
    {     
            var l = new List<int>();     
            for (int i = 0; i < 100; i++)     
            {         
                  l.Add(i);     
            }     
            
            return l; 
    }
    And also there is another method using yield,
    Code:
    public static IEnumerable<int> GetResult() 
    {     
           for (int i = 0; i < 100; i++)     
           {         
                  yield return i;     
           } 
    }
    My question what GetResult does? Does it achieve the same thing as GetResultList? Thanks.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,383

    Re: A question regarding yield break and yield return

    Quote Originally Posted by LarryChen View Post
    My question what GetResult does? Does it achieve the same thing as GetResultList? Thanks.
    This is the part of the recurring theme where I encourage you to write code instead of asking questions. In answer to your question, the article explains it pretty well and the answer is 'yes'. However, don't take my word for it - write a couple of lines of code and verify it yourself.

  3. #3
    Join Date
    Jul 2005
    Posts
    936

    Re: A question regarding yield break and yield return

    Quote Originally Posted by Arjay View Post
    This is the part of the recurring theme where I encourage you to write code instead of asking questions. In answer to your question, the article explains it pretty well and the answer is 'yes'. However, don't take my word for it - write a couple of lines of code and verify it yourself.
    Actually I tried to test the code from the article,
    Code:
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                IEnumerable<string> ret = GetMoreResults();
            }   
         
            public static IEnumerable<string> GetMoreResults() 
            {     
                for (int i = 0; i < 20; i++)     
                {         
                    yield return "Value " + i;         
                    yield break;     
                }        //Do something else           
                
                for (int i = 0; i < 20; i++)     
                {         
                    yield return "Another value " + i;     
                } 
            }
        }
    }
    I set the breakpoint at the very beginning of GetMoreResults but it is never hit. Why? So I didn't get a chance to understand what is really going on with GetMoreResults.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,383

    Re: A question regarding yield break and yield return

    Quote Originally Posted by LarryChen View Post
    I set the breakpoint at the very beginning of GetMoreResults but it is never hit. Why? So I didn't get a chance to understand what is really going on with GetMoreResults.
    That's a different question now isn't it? At any rate, bing or google "yield break C# example" for some good explanations.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center