Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq expression with multiple input parameters

Tags:

c#

.net

linq

I wrote the following code and I'm not able to understand how it is executed.

class Program
{
    static void Main(string[] args)
    {
        int[] nums = { 1, 5, 10, 3, 554, 34};
        var nn = nums.TakeWhile((n, junk) => n > junk);
        nn.Count();
        foreach (var a in nn)
        {
            Console.WriteLine(a);
        }
        Console.ReadKey();
    }
}

First I wrote the TakeWhile expression as n => n > 5. I'm able to understand that. But I just added one more parameter junk. What is junk here? What value is assigned to it during query exeution? How is it giving the output as 1, 5 and 10.

like image 399
NLV Avatar asked Aug 19 '26 17:08

NLV


1 Answers

junk is the index of of the currently processed element - see http://msdn.microsoft.com/en-us/library/bb548775.aspx

Much more readable and understable would be

var nn = nums.TakeWhile((n, index) => n > index);
like image 197
Yahia Avatar answered Aug 22 '26 06:08

Yahia



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!