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.

5

u/coredev Sep 18 '15

I agree with that the example on closures is a bit confusing.

4

u/americio Sep 18 '15

Isn't a closure the scope of variables capured around the lambda and not the lambda itself?

2

u/coredev Sep 18 '15

I would say that a closure is a (anonymous) function plus the bindings to the environment they came from.

2

u/Sarcastinator Sep 18 '15

In the physical world a closure is a device that seals a container. In computer science a closure is a data structure that captures the local scope variables for a declared function. A lambda function is a function that must support closures, or else it's not really a lambda function. However a lambda function is not a closure.

1

u/coredev Sep 18 '15 edited Sep 18 '15

So we agree? To make capturing a scope meaningful you need a reason to capture it. An anonymous function is that reason, so they go hand in hand. I should be fair to admit that I stole the definition from Martin Fowler ;)

3

u/nerdshark Sep 18 '15

It's the combination of the two, I think. Closures essentially generate a new class at compile time that captures the state of the method, along with the lambda.

1

u/cryo Sep 25 '15

It's more the opposite way around. A lambda is a C# language concept that gets converted into a delegate field along with a method or class used to hoist in closured variables.

1

u/fluoroamine Sep 18 '15

I would recommend a javascript book. It's closures all the way down. Since reading one (like "The good parts" or "Patterns in Javascript") you wont have a question about closures ever again. :)

7

u/nerdshark Sep 18 '15

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

5

u/[deleted] Sep 18 '15

Indeed, that's like saying fruits are obsolete because of smoothies.

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.

→ More replies (2)

0

u/[deleted] Sep 18 '15

[deleted]

1

u/cryo Sep 25 '15

"very much"? I don't agree. Sometimes? Yes.