C # create an infinite sequence (delayed loading)

Source: Internet
Author: User

An infinite sequence means that a sequence has an infinite number of elements. This seems to be certain that the memory in the general program is limited and cannot generate an infinite sequence. In fact, the infinite sequence here refers to the ability to take any number of elements. Conventional Methods Cannot initialize a sequence. Therefore, curves can be implemented only in other ways.

You can use the IEnumerable interface to implement infinite sequences. Because the IEnumerable interface inherits from IEnumerator, an iterator is implemented. That is to say, the instance interface of IEnumerable cannot be instantiated. This indicates the instance of the class that implements IEnumerable, you can use iterative methods to traverse its elements in sequence. In addition, the elements in the IEnumerable instance are not included in the definition, but calculated only when it is actually used. Therefore, we can define an infinite sequence when defining it. In actual use, it must be just a few elements in this infinite sequence, in this way, you only need to iterate these elements. This is the benefit of delayed loading.

An infinite Fibonacci sequence is defined here.

static IEnumerable<int> fib()       {           int pre = 0;           int next = 1;           while (true)           {               var val = pre + next;               yield return val;               pre = next;               next = val;           }       }

At first glance, when we see while (true), we think this is an endless loop. Otherwise, there is a yield in it, which will throw a value for each iteration. While (true) contains the loop required for iteration, because it is an endless loop, that is, it can be iterated countless times. Of course, if you change to a for loop and set the number of cycles to 10, you can only iterate 10 times. This is just a common finite sequence.

When using this sequence, you can:

Var f = fib (); // the elements in the calculation are not actually started at this time.

If you want to Take 10 elements, you can use the extension method of linq to Take (10) to Take the elements, but you must know that the Take (10) function does not actually Take 10 numbers, these elements are computed only when they are actually used.

var list=f.Take(10);

The following code traverses this sequence to calculate the element value.

foreach (var i in  list ){       Console.WriteLine(i); }

Or you can directly call ToList after Take (10), so that the sequence can calculate the elements immediately.

This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/1229663

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.