public class Generator
{
public static IEnumerable<T> Generate<T>(Func<T> func)
{
while (true)
{
yield return func();
}
}
public static IEnumerable<T> Generate<T>(Func<int, T> func)
{
int index = 0;
while (true)
{
yield return func(index);
index++;
}
}
}Dzięki temu możemy w łatwy sposób stworzyć listę 10 elementów z losowymi i ponumerowanymi liczbami.
Random r = new Random();
var listWithRandomElements = Generator.Generate(()=> r.Next(10))
.Take(10);
var listWithNumberedElements = Generator.Generate(i => i)
.Take(10);
Brak komentarzy:
Prześlij komentarz