C# – Property with Enumerable or list

cenumerablelinqlist

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?

Best Answer

An important difference between the two is that with your second version you have to know in advance when you want to stop, but in the first version you can start iterating and later decide when to stop. You don't have to know in advance.

You also don't have to store the entire list in memory at once with the first version. You can handle the data in a streaming manner.

An advantage of the second is that returning a list allows you to index into the array rather than handling elements one by one from the beginning. You could use the second version if you do know how many elements you want, and you know that the list is small enough to fit in memory.

Note that these differences have nothing to do with whether you use a property or a function call. You could rewrite the first to be a function call that takes no parameters.

If you have only the first version available, you could easily emulate the second version by using Fibinocci().Take(20).ToList().