.net - Discrepancy in C# LINQ results -
when this:
currentpage = metadataresponse.applicationtype.pages.find( page => page.sortorder == ++currentpage.sortorder);
the value of currentpage
null.
but same logic, when assign increment value integer variable, , try currentpage
int sortorder = ++currentpage.sortorder; currentpage = metadataresponse.applicationtype.pages.find( page => page.sortorder == sortorder);
currentpage
gets populated.
does have answer why 1 works , other doesn't?
note: assume find
method applied collection of values.
in first code example, incrementing currentpage
each element in collection (this happening because lambda expressions introduce closures on variables captured outer scope - see below code block more info that). in second code example, currentpage
incremented once. take how following program behaves:
class program { static void main(string[] args) { func1(); console.writeline("\n"); func2(); console.readkey(); } private static void func1() { int = 0; var list = new list<int> { 1, 2, 3 }; list.foreach(x => console.writeline(++i)); } private static void func2() { int = 0; int j = ++i; var list = new list<int> { 1, 2, 3 }; list.foreach(x => console.writeline(j)); } }
here more info closures in lambda expressions. have fun! ;-)
Comments
Post a Comment