Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Created February 10, 2014 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tugberkugurlu/8918610 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/8918610 to your computer and use it in GitHub Desktop.
Simple C# yield return sample
class Program
{
static void Main(string[] args)
{
IEnumerable<string> values = GetStrings().Take(10);
foreach (string value in values)
{
Console.WriteLine(value);
}
Console.ReadLine();
}
static IEnumerable<string> GetStrings()
{
for (int i = 0; i < 100; i++)
{
yield return GetString();
}
}
static string GetString()
{
Thread.Sleep(1000);
string value = new Random().Next().ToString();
Console.WriteLine(value);
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment