LINQ 4th (skipwhile and takewhile)

Source: Internet
Author: User

The example in the book is normal, so I wrote something abnormal.

This is what I wrote.

    class Program    {        static void Main(string[] args)        {            int[] mn = { 1,2,3,4,5,6,7,8,9};            var z = mn.TakeWhile((x, i) => i < 5);            foreach(var x in z)            {                Console.WriteLine(x);            }            Console.ReadLine();        }    }

The result is
1

2

3

4

5

I changed I <5 to I> 5, so there is no output.

The high person reminds me that I is index and X is the value.

Some people said:

When the first value is determined, if the returned value is false, skipwhile/takewhile is terminated.

Note: Both takewhile and skipwhile execute actions based on the concept of while.

 

Then, I understand...

    class Program    {        static void Main(string[] args)        {            int[] mn = { 1,2,6,3,4,5,6,7,8,9};            var z = mn.TakeWhile((x, i) => i< 5);            foreach(var x in z)            {                Console.WriteLine(x);            }            Console.ReadLine();        }    }
View code

 

Running result


 

    class Program    {        static void Main(string[] args)        {            int[] mn = { 1,2,6,3,4,5,6,7,8,9};            var z = mn.TakeWhile((x, i) => x<5);            foreach(var x in z)            {                Console.WriteLine(x);            }            Console.ReadLine();        }    }
View code

 

What is output?

 

 

1

2

It's amazing ~

 

 

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.