Tony Johansson
2016-05-04 09:44:13 UTC
Hello!
I have this task from a book and I'm a little bit unsure how to understand
it. It's quite vague.
[Demonstrate how to use iterator blocks to implement a function that takes a
sequence of integers as input, doubles each element and generates a new
sequence of doubled elements.]
Would you consider my solution to satisfy the task.
public class Example
{
static void Main()
{
List<int> result = new List<int>();
int[] ints = { 1, 2, 3, 4, 5 };
foreach (int number in DoubleEach(ints))
{
result.Add(number);
}
}
public static System.Collections.Generic.IEnumerable<int>
DoubleEach(IEnumerable<int> ints)
{
foreach(int tal in ints)
{
yield return tal*2;
}
}
}
//Tony
I have this task from a book and I'm a little bit unsure how to understand
it. It's quite vague.
[Demonstrate how to use iterator blocks to implement a function that takes a
sequence of integers as input, doubles each element and generates a new
sequence of doubled elements.]
Would you consider my solution to satisfy the task.
public class Example
{
static void Main()
{
List<int> result = new List<int>();
int[] ints = { 1, 2, 3, 4, 5 };
foreach (int number in DoubleEach(ints))
{
result.Add(number);
}
}
public static System.Collections.Generic.IEnumerable<int>
DoubleEach(IEnumerable<int> ints)
{
foreach(int tal in ints)
{
yield return tal*2;
}
}
}
//Tony