Accepted answer

that particular overload of the where extension method is defined to accept a func where the first parameter is the element and the second parameter is the index of that element. this is mentioned explicitly in the documentation:


type: system.func<tsource, int32, boolean>

a function to test each source element for a condition; the second parameter of the function represents the index of the source element.

emphasis mine.


because there is an overload of where taking as a predicate a function like this:

func<tsource, int, bool>

the semantic of this predicate ios defined so the second parameter is the index. so if you pass a lambda with two args that version of where is called.


the overload of where that you are using is implemented like this:

public static ienumerable<t> where<t>(
      this ienumerable<t> list, func<t,int,bool> predicate)
   int num = -1;
   foreach (t current in list)
      if (predicate(current, num))
         yield return current;
   yield break;

as you can see, on each iteration, the predicate (i.e. the passed lambda expression) is invoked passing the current element of the list and the current index.

in this way, the code in the lambda expression know both the element and its index.


this is just a naming thing, you could name digit anything, and index anything. this is just a lambda expression for an anonymous could be rewritten:

(string digit, int index) => digit.length < index

so, the names are the same as any parameter you normally set up in a method. there is nothing truly special about the names being recognized by the c# engine.

(string index, int digit) => index.length < digit

the above would work would be confusing, but it would work. it is just to show that the name can be whatever you want

if you are referring how to the signature itself then it is due to an overload of the where function

public static ienumerable<tsource> where<tsource>(
    this ienumerable<tsource> source,
    func<tsource, int, bool> predicate

so, the tsource in this case is string, making the func become func<string, int, bool>. meaning that the lambda must take a string parameter followed by an int param, and return a bool value

Related Query

More Query from same tag