Linq Query:
Example:
//TakeWhile Example
int[] myNumbers = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,0,1,2 };
var numbslessthan6 = myNumbers.TakeWhile(n => n < 6);
foreach (var t in numbslessthan6)
{
Console.WriteLine(t.ToString());
}
Output: 0
1
2
3
4
5
- Its like using a break in for loop.. here it will loop through all the myNumbers and if the number is less than 6 than its added to numbslessthan6.
- Whenever a number greater than 6 in encountered the loop breaks even if there are number less than 6 after the current number which is greater than 6
Example:
for (int i = 0; i < myNumbers.Length; i++)
{
if (i < 6)
{
Console.WriteLine(i.ToString());
}
else
{
break;
}
}
Output:0
1
2
3
4
5

0 comments:
Post a Comment