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 ~