I'm playing around with LINQ and related subjects and was wondering about the following.
I've 2 methods of getting a Fibonacci sequence. I started with:
public static IEnumerable<int> Fibonacci
{
get
{
int i = 0;
int j = 1;
int temp = 0;
while (true)
{
yield return i;
temp = i;
i = j;
j = temp + i;
}
}
}
But it got me thinking, why would I choose this over:
public static IList<int> Fibonacci(long to)
{
IList<int> fibList = new List<int>();
int i = 0;
int j = 1;
int temp, index = 0;
while (index < to)
{
fibList.Add(i);
temp = i;
i = j;
j = temp + i;
index++;
}
return fibList;
}
IList is an Enumerable as well + I might want to add some parametrization to it. I'm not really looking for optimisations or stuff or thing like use < long > because the number gets big fast, its merely a quick example. Just some arguments pro and con each method. Why and when should I use which method?