Skip to content

Instantly share code, notes, and snippets.

@buddhika85
Last active August 25, 2017 05:56
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 buddhika85/63ef6353928e6b0df98949171790f6a4 to your computer and use it in GitHub Desktop.
Save buddhika85/63ef6353928e6b0df98949171790f6a4 to your computer and use it in GitHub Desktop.
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