Last active
August 25, 2017 05:56
-
-
Save buddhika85/63ef6353928e6b0df98949171790f6a4 to your computer and use it in GitHub Desktop.
This file contains 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<int> result = PowerMe(2, 3); | |
foreach (var item in result) | |
{ | |
Console.WriteLine(item); | |
} | |
Console.ReadKey(); | |
} | |
// Returns IEnumerable | |
// Like streaming video | |
// When an item is created in IEnumerable, it returns the single item and comes back | |
// Yield keyword remembers where it has to start for the next iteration | |
static IEnumerable<int> PowerMe(int number, int exponent) | |
{ | |
int result = 1; | |
for (int i = 0; i < exponent; i++) | |
{ | |
result = result * number; | |
yield return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment