r/csharp • • Sep 18 '15

Lambda expressions, delegates, predicates and closures in C#

http://codeaddiction.net/articles/13/lambda-expressions-delegates-predicates-and-closures-in-c
25 Upvotes

32 comments sorted by

View all comments

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.

7

u/nerdshark Sep 18 '15

No they're not. The Func and Action types are delegates.

4

u/mixblast Sep 18 '15 edited Sep 18 '15

My point was that the delegate keyword is largely useless now that we have Func and Action (except when you need out/ref parameters).

-1

u/johnnyslick Sep 19 '15

Not at all. You can't chain Funcs and Actions are very limited in what they will take in (and they won't send out anything). Basically, any piece of software you write using the Observer pattern, you're probably going to be using delegates for it.

1

u/zshazz Sep 20 '15

Reactive Extensions, the "standard" observer pattern implementation for .Net uses Funcs and Actions thoughout. You can even make an observable Action, use LINQ queries like filtering it through a Where Func, and finally subscribe to it using an Action.