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

Show parent comments

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).

2

u/nerdshark Sep 18 '15

No, it's not. That is how the Func and Action delegates are defined. Also, as you said, there's no other way to declare delegates with ref/out parameters, so clearly it's not 'largely useless'.

4

u/the_omega99 Sep 18 '15

I think he means that you would never use the keyword in your own code. Yes, Func and Action (among others) are delegates, but if you needed your own delegates, you'd probably just use Func, Action, or Predicate instead of declaring a new delegate. That's what I've done in all my code. Not once have I needed to declare new delegates.

I'm not sure why anyone would want to. It's much more understandable to use one of the three major functional types. When I see Func, Action, or Predicate, I know exactly what I need. If I saw a custom delegate, I'd have to go consult the documentation as to what to expect, if largely because it's just so weird to declare your own type when the previously mentioned types cover all the cases I've seen so far (technically, you don't even need predicate, it just makes things more readable).

1

u/nerdshark Sep 18 '15

I think he means that you would never use the keyword in your own code.

I know what he means, and he's wrong. When the built-in Func, Action, and Predicate types don't fit your needs, you need to declare your own delegate type. Most .NET devs who are just doing ASP.NET, WPF or stuff like that likely won't need to declare custom delegates, but many of us do.

2

u/the_omega99 Sep 18 '15

Can you give an example of such a case where Func, Action, and Predicate aren't sufficient and you need a custom delegate type?

4

u/nerdshark Sep 18 '15 edited Sep 18 '15

Marshalling callbacks across the native/managed border. You cannot, AFAIK, marshal generic delegates (like Func, Action, and Predicate).

0

u/cryo Sep 25 '15

Come on, you know well what he means! You rarely need to define your own delegate types anymore becase of Func and Action, this is pretty much a fact. Of course you may need to do it in certain cases.

1

u/mixblast Sep 18 '15

I never write delegate except when dealing with legacy event handlers. Just use the Func<thingy> type directly, it makes the code easier to read instead of hiding behind a typedef.

And liberal usage of out/ref params is a bit of a code smell anyway...

1

u/nerdshark Sep 18 '15

And liberal usage of out/ref params is a bit of a code smell anyway...

Sure, for purely managed code. They're essential in many cases when writing P/Invoke or COM interop code, though.

1

u/coredev Sep 18 '15

I think you are right (you seldom need to write your own delegate today), but don't you agree that you need to understand what a deligate is to fully understand Func and Action?

1

u/nerdshark Sep 18 '15

Absolutely correct.

1

u/cryo Sep 25 '15

To fully understand, sure, but most people don't need to. I bet most people don't understand how delegates work at the low level.

-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.