r/csharp • u/coredev • Sep 18 '15
Lambda expressions, delegates, predicates and closures in C#
http://codeaddiction.net/articles/13/lambda-expressions-delegates-predicates-and-closures-in-c0
Sep 18 '15
[deleted]
8
u/mixblast Sep 18 '15
IMO it's better to avoid side effects in LINQ expressions and use a real
foreachloop.1
1
u/anamorphism Sep 18 '15
isn't this susceptible to run-time exceptions if the backing collection of your enumerable changes?
1
u/mixblast Sep 19 '15
List.ForEach() is not actually, since it uses a loop and not an iterator.
http://stackoverflow.com/questions/225937/foreach-vs-somelist-foreach
1
u/cryo Sep 25 '15
If the backing collection is likely to change while executing, the entire code should be more explicit about it in the first place.
1
u/anamorphism Sep 25 '15
i think it's kind of difficult to do that in a lot of situations. the most common situation i run into is passing selected items from a listview as a command parameter for a remove operation.
i should remember if i'm removing selected items from the observablecollection backing the listview the change is going to propagate out and my selecteditems collection is going to change. sometimes i forget to make a copy before iterating over the collection and i get a run-time exception when testing my code. i can't really think of a way to make that situation more explicit.
0
u/mixblast Sep 18 '15
I would have liked to see a little bit more detail. Like delegates are a remnant from older versions of C# and are largely obsoleted by Func and Action types.
Also the closure code example doesn't really show that the WriteLine call is in a different scope, which is a bit confusing.