Created
February 10, 2014 16:08
-
-
Save tugberkugurlu/8918610 to your computer and use it in GitHub Desktop.
Simple C# yield return sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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