-
July 3rd, 2013, 10:21 AM
#1
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.
-
July 3rd, 2013, 11:29 AM
#2
Re: A question regarding yield break and yield return
Originally Posted by LarryChen
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.
-
July 3rd, 2013, 01:09 PM
#3
Re: A question regarding yield break and yield return
Originally Posted by Arjay
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.
-
July 3rd, 2013, 01:58 PM
#4
Re: A question regarding yield break and yield return
Originally Posted by LarryChen
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|