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.
Re: A question regarding yield break and yield return
Quote:
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.
Re: A question regarding yield break and yield return
Quote:
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.
Re: A question regarding yield break and yield return
Quote:
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.