Alex Rivera | Logout

Outer Variable Trap

Asked 2010-08-05T16:08:48.580
50

What exactly is the Outer Variable Trap? Explanation and examples in C# are appreciated.

EDIT: Incorporating Jon Skeet's diktat :)

Eric Lippert on the Outer Variable Trap

Edit
Report

1 Answer

5

Something like

foreach (var s in strings)
    var x = results.Where(r => (r.Text).Contains(s));

Will not give the results you're expecting because the Contains is not executed for each iteration. Assigning s to a temporary variable inside the loop will fix this, though.

answered 2010-08-05T16:16:01.203

Your Answer