c# - Linq .Select() / .SelectMany() automatically uses second, optional parameter -


i found weirdest behavior in linq:

when calling unary functions pass function name, instead of

var foo = mylist.select(item => myfunc(item)); 

i write

var foo = mylist.select(myfunc); 

which should same. in cases, isn't! namely if function has second parameter int , optional:

private string myfunc(string input, int foo = 0) {    ... } 

in case, statement

var foo = mylist.select(myfunc); 

equals

var foo = mylist.select((item, index) => myfunc(item, index)); 

if second parameter either not opional or not int, compiler complains, in case, sneakily surprises you.

has else encountered this? other linq expressions work way? (so far, .selectmany() does). , elegant way work around behavior (and keep others falling same trap?)

this not issue of specific linq extension method, how optional parameters handled funcs , actions, in short - not, considered regular parameter , default value omitted when selecting corresponding func/action signature. take here optional parameters, no overload 'employee' matches delegate 'system.func<employee> or here invoke func<t1, t2, t3> has optional parameters?.

in other words, myfunc cannot used func<string, string>, must use func<string, int, string>, in case of select happens present overload index added.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -