c# - Property with Enumerable or list -


I'm playing with LINQ and related topics and was thinking about the following.

There are 2 ways to get a Fibonacci sequence I started:

  Public Fixed IEnumerable & lt; Int & gt; Fibonacci {i {ii = 0; Int j = 1; Int temp = 0; While (true) {yield returns i; Temp = i; I = J; J = temp + i; }}}  

But I got to think about this, why would I choose it:

  public static IList & lt; Int & gt; Fibonacci (long time) {IList & lt; Int & gt; Fidel list = new list & lt; Int & gt; (); Int i = 0; Int j = 1; Int temp, index = 0; Whereas (from index & lt;) {fibList.Add (i); Temp = i; I = J; J = temp + i; Index ++; } Return Fibliƫl; }  

ILIT is also a countable + I would like to add some parametrations for this. I'm not really looking for things like optimization or stuff or use & lt; Long> Because the numbers grow very fast, it is only a quick example of something just logic and understand each method, what other method should I use?

One important difference between the two is that with your second version you will need to know in advance when you You want to stop, but in the first version you can start again and later decide when you are closed you do not have to know in advance.

You do not need to store the entire list in memory with the first version once. You can handle the data in a streaming way. One advantage of the other is that returning a list allows you to index the elements rather than assuming the elements one by one from the beginning. If you know how many elements you want, then you can use the second version, and you know that the list is small enough to fit in the memory.

Note that there is nothing to do with these differences, whether you call property or function, you can rewrite the first function call, which does not take any parameters.

If you have only the first version available, you can easily simulate the second version using Fibinocci (). ) .list () .


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -