in an interview , interviewer ask me following query
int[] array = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Func<int, int> func = i =>
{
Console.Write(array[i]);
return i;
};
var result = array.Where(e => e <= func(2)).ToArray();
so will any one guide me how e <= func(2) thing works?
and how last line i.e
var result = array.Where(e => e <= func(2)).ToArray();
works?
It may be easier to understand if you use parentheses:
This
Constructs a function which takes one argument.
This
compares
etofunc(2).func(2)calls the functionfuncwith the argument2.All in all, the
<=has nothing to do with=>. They are two completely different operators. To summarize... => ...constructs a function.... <= ...compares the arguments.