473,394 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

The real difference between lambda expression and anonymous delegates

Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?
>From the following example that I tried, I couldn't derive any real
advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);

Thanks,
Matt

Jun 22 '07 #1
15 4810
Lit
I like the Lambda because it saves time, typing and trees when printing.
"Matt" <mu****************@gmail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?
>>From the following example that I tried, I couldn't derive any real
advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);

Thanks,
Matt

Jun 22 '07 #2
Matt wrote:
Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?
>>From the following example that I tried, I couldn't derive any real
advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);

Thanks,
Matt
Hi Matt,

Functionally: none. It really is just syntactic sugar. However, there
advantages to using either approach in your code.

For example, lambda expressions enable concise, small and readable code when
small expressions are being, err, expressed. Also, it provides a nice
bridge between functional and imperative/procedural programming.

However, when you have a really complex task to perform, it may be wiser
and/or necessary to use a delegate.

Your question was what's the advantage of lambda expressions over anonymous
delegates, however, if you asked what's the advantage of anonymous
delegates over lambda expressions, I'd say that they allow more programming
constructs to go in your anonymous routine (such as scoped variable
declaration, multiple statement execution) and represent this as a function
within a function.

--
Tom Spink
University of Edinburgh
Jun 22 '07 #3
Tom,

Lambda Expressions actually are different functionally than delegates.
With lambda expressions, you can do this:

Expression<Predicate<int>e = i =i 5;

You can't do that with a delegate (assign it to an Expression<T>). When
you compile this code, the compiler creates an expression tree, where the
model can be accessed which gives the details of the expression.

You can convert the expression to a delegate by calling:

Predicate<intgreaterThanFive = e.Compile();

Of course, if you assign the lamba expression to a delegate, the
compiler creates an anonymous delegate all the same.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Tom Spink" <ts****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Matt wrote:
>Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?
>>>From the following example that I tried, I couldn't derive any real
advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);

Thanks,
Matt

Hi Matt,

Functionally: none. It really is just syntactic sugar. However, there
advantages to using either approach in your code.

For example, lambda expressions enable concise, small and readable code
when
small expressions are being, err, expressed. Also, it provides a nice
bridge between functional and imperative/procedural programming.

However, when you have a really complex task to perform, it may be wiser
and/or necessary to use a delegate.

Your question was what's the advantage of lambda expressions over
anonymous
delegates, however, if you asked what's the advantage of anonymous
delegates over lambda expressions, I'd say that they allow more
programming
constructs to go in your anonymous routine (such as scoped variable
declaration, multiple statement execution) and represent this as a
function
within a function.

--
Tom Spink
University of Edinburgh
Jun 22 '07 #4
Nicholas Paldino [.NET/C# MVP] wrote:
Tom,

Lambda Expressions actually are different functionally than delegates.
With lambda expressions, you can do this:

Expression<Predicate<int>e = i =i 5;

You can't do that with a delegate (assign it to an Expression<T>).
When
you compile this code, the compiler creates an expression tree, where the
model can be accessed which gives the details of the expression.

You can convert the expression to a delegate by calling:

Predicate<intgreaterThanFive = e.Compile();

Of course, if you assign the lamba expression to a delegate, the
compiler creates an anonymous delegate all the same.
Hi Nicholas,

Thanks for the clarification there! That's quite interesting, and really
cool. :)

--
Tom Spink
University of Edinburgh
Jun 22 '07 #5

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:28**********************************@microsof t.com...
Tom,

Lambda Expressions actually are different functionally than delegates.
With lambda expressions, you can do this:

Expression<Predicate<int>e = i =i 5;

You can't do that with a delegate (assign it to an Expression<T>).
When you compile this code, the compiler creates an expression tree, where
the model can be accessed which gives the details of the expression.
That presumbly enables a computer algebra system? Like symbolic
differentiation?
>
You can convert the expression to a delegate by calling:

Predicate<intgreaterThanFive = e.Compile();

Of course, if you assign the lamba expression to a delegate, the
compiler creates an anonymous delegate all the same.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Tom Spink" <ts****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>Matt wrote:
>>Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?
From the following example that I tried, I couldn't derive any real
advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);

Thanks,
Matt

Hi Matt,

Functionally: none. It really is just syntactic sugar. However, there
advantages to using either approach in your code.

For example, lambda expressions enable concise, small and readable code
when
small expressions are being, err, expressed. Also, it provides a nice
bridge between functional and imperative/procedural programming.

However, when you have a really complex task to perform, it may be wiser
and/or necessary to use a delegate.

Your question was what's the advantage of lambda expressions over
anonymous
delegates, however, if you asked what's the advantage of anonymous
delegates over lambda expressions, I'd say that they allow more
programming
constructs to go in your anonymous routine (such as scoped variable
declaration, multiple statement execution) and represent this as a
function
within a function.

--
Tom Spink
University of Edinburgh
Jun 22 '07 #6
Hello Nicholas,
Thanks for your answer. Is there any way to use the lambda expression
other than compiling it as a delegate and then using it? What would be
an appropriate use case for such an expression? I had used anonymous
delegates extensively when dealing with "InvokeRequired" in ui code.
It would be great if you can point such a use case for the lambda
expression (Expression<Predicate<int>e = i =i 5;).
Thanks,
Matt

Jun 22 '07 #7
"Matt" <mu****************@gmail.comschrieb im Newsbeitrag
news:11**********************@e9g2000prf.googlegro ups.com...
Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?
>>From the following example that I tried, I couldn't derive any real
advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);
The lambda expression is more readable IMO.

Christof
Jun 22 '07 #8
"Matt" <mu****************@gmail.comschrieb im Newsbeitrag
news:11**********************@q19g2000prn.googlegr oups.com...
Hello Nicholas,
Thanks for your answer. Is there any way to use the lambda expression
other than compiling it as a delegate and then using it?
It can be used in LINQ, where the expression tree could be resolved e.g. to
a filter condition in a SQL-Statement.

Christof
Jun 22 '07 #9
Matt,

Well, you can do this as well:

Predicate<intgreaterThanFive = i =i 5;

And the compiler will compile it into an anonymous delegate.

There are two main benefits for lambda expressions. The first is the
ability to do this:

ParameterExpression param = Expression.Parameter(typeof(int), "i");
Expression greaterThan = Expression.GreaterThan(param,
Expression.Constant(5));
LambdaExpression greaterThanFive =
Expression.Lambda<Predicate<int>>(greaterThan, param);

// Compile.
Predicate<intcheck = (Predicate<int>) greaterThanFive.Compile();

// Compare.
Console.WriteLine(check(10));
Console.WriteLine(check(4));

This will allow you to dynamically create and execute expressions in
code. Dynamic code generation becomes a lot simpler with this (at least,
simple code generation).

Given that lambda expressions can be pulled apart into an object model
and analyzed, when you do this:

Expression<Predicate<int>e = i =i 5;

You can look at the components of the expression, so that you can
convert the query syntax into another language, which is in fact, exactly
what LINQ to SQL does.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Matt" <mu****************@gmail.comwrote in message
news:11**********************@q19g2000prn.googlegr oups.com...
Hello Nicholas,
Thanks for your answer. Is there any way to use the lambda expression
other than compiling it as a delegate and then using it? What would be
an appropriate use case for such an expression? I had used anonymous
delegates extensively when dealing with "InvokeRequired" in ui code.
It would be great if you can point such a use case for the lambda
expression (Expression<Predicate<int>e = i =i 5;).
Thanks,
Matt
Jun 22 '07 #10
To a limited degree, yes, in that you are able to pass around code as
data, but only to a limited degree, of course.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ben Voigt [C++ MVP]" <rb*@nospam.nospamwrote in message
news:E9**********************************@microsof t.com...
>
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:28**********************************@microsof t.com...
>Tom,

Lambda Expressions actually are different functionally than delegates.
With lambda expressions, you can do this:

Expression<Predicate<int>e = i =i 5;

You can't do that with a delegate (assign it to an Expression<T>).
When you compile this code, the compiler creates an expression tree,
where the model can be accessed which gives the details of the
expression.

That presumbly enables a computer algebra system? Like symbolic
differentiation?
>>
You can convert the expression to a delegate by calling:

Predicate<intgreaterThanFive = e.Compile();

Of course, if you assign the lamba expression to a delegate, the
compiler creates an anonymous delegate all the same.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Tom Spink" <ts****@gmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>>Matt wrote:

Hi There,
Can anyone explain me the real advantages of (other than syntax)
lambda expressions over anonymous delegates?
>From the following example that I tried, I couldn't derive any real
advantage for one over the other.

delegate int F(int a);

F fLambda = a =a++;
F fAnonymous = delegate(int a) { return a++; };
fLambda(1);
fAnonymous(1);

Thanks,
Matt

Hi Matt,

Functionally: none. It really is just syntactic sugar. However, there
advantages to using either approach in your code.

For example, lambda expressions enable concise, small and readable code
when
small expressions are being, err, expressed. Also, it provides a nice
bridge between functional and imperative/procedural programming.

However, when you have a really complex task to perform, it may be wiser
and/or necessary to use a delegate.

Your question was what's the advantage of lambda expressions over
anonymous
delegates, however, if you asked what's the advantage of anonymous
delegates over lambda expressions, I'd say that they allow more
programming
constructs to go in your anonymous routine (such as scoped variable
declaration, multiple statement execution) and represent this as a
function
within a function.

--
Tom Spink
University of Edinburgh
Jun 22 '07 #11
Nicholas, Christof, Tom, Ben,
Thanks for you all.
Matt

Jun 22 '07 #12
On Jun 21, 9:28 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
That presumbly enables a computer algebra system? Like symbolic
differentiation?
Well, except that a CAS[1] really has the ability to reduce and
simplify expressions. That ability requires specialized data
structures that I'm thinking are incompatible with how the expression
tree is stored. You may be able to do trivial simplifications, but
monomial and polynomial simplification and especially differentiation,
factorization, and integration would be too difficult if not
impossible. It's probably easier to just write the CAS compiler and
operations from scatch.

I still have not played around much with C# 3.0. Can lambda
expression trees even be compiled from a string input?

[1] I do not consider a trivial expression evaluator to be a CAS.

Jun 22 '07 #13
Tom Spink <ts****@gmail.comwrote:

<snip>
Your question was what's the advantage of lambda expressions over anonymous
delegates, however, if you asked what's the advantage of anonymous
delegates over lambda expressions, I'd say that they allow more programming
constructs to go in your anonymous routine (such as scoped variable
declaration, multiple statement execution) and represent this as a function
within a function.
This is allowed within lambda expressions too, although the
functionality is relatively rarely used. Here's a short but complete
example:

using System;
using System.Threading;

class Program
{
static void Main()
{
ThreadStart ts = () =>
{
DateTime date = DateTime.Today;
date = date.AddDays(10);
Console.WriteLine(date);
};

new Thread(ts).Start();
}
}

It's relatively rare to see this kind of thing in examples at the
moment, admittedly - usually a single expression (not even a whole
statement, necessarily) is used, particularly in LINQ examples.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 23 '07 #14

"Brian Gideon" <br*********@yahoo.comwrote in message
news:11*********************@w5g2000hsg.googlegrou ps.com...
On Jun 21, 9:28 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
>That presumbly enables a computer algebra system? Like symbolic
differentiation?

Well, except that a CAS[1] really has the ability to reduce and
simplify expressions. That ability requires specialized data
structures that I'm thinking are incompatible with how the expression
tree is stored. You may be able to do trivial simplifications, but
monomial and polynomial simplification and especially differentiation,
factorization, and integration would be too difficult if not
impossible. It's probably easier to just write the CAS compiler and
operations from scatch.

I still have not played around much with C# 3.0. Can lambda
expression trees even be compiled from a string input?

[1] I do not consider a trivial expression evaluator to be a CAS.
Well, being able to evaluate a derivative of an arbitrary expression without
resorting to a difference quotient approximation is already useful IMHO,
even without simplification. But additionally having the C# compiler
generate an expression tree would simply a CAS considerably even if that
isn't the final form used for manipulation, plus the API is given much
cleaner syntax.

Jun 26 '07 #15
On Jun 25, 7:55 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
Well, being able to evaluate a derivative of an arbitrary expression without
resorting to a difference quotient approximation is already useful IMHO,
even without simplification. But additionally having the C# compiler
generate an expression tree would simply a CAS considerably even if that
isn't the final form used for manipulation, plus the API is given much
cleaner syntax.
I'm not seeing that the C# compiler can dynamically generate the
expression trees from user input though. Don't you still need to
provide a lexer and parser for that? Even if it can I'm afraid the
language it would accept would be very (if not entirely) C#'ish which
isn't ideal for a CAS. It would, however, be sufficient for an
expression evaluator which is still quite useful :)
Jun 26 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
6
by: George Yoshida | last post by:
Just out of curiosity, is there any way to get the name of a lambda expression? Lambdas are anonymous functions, so it's a stupid idea to get the name of it. But if it's possible, how could I? ...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
30
by: Mike Meyer | last post by:
I know, lambda bashing (and defending) in the group is one of the most popular ways to avoid writing code. However, while staring at some Oz code, I noticed a feature that would seem to make both...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
8
by: Simon Woods | last post by:
Hi A blog post I was reading (http://www.defmacro.org/ramblings/fp.html) included this lambda function. Function makeIncrementer() { int n = 0; int increment() { return ++n;
20
by: raylopez99 | last post by:
Took a look at all the fuss about "lambda expressions" from Jon Skeet's excellent book "C# in Depth". Jon has an example, reproduced below (excerpt) on lambda expressions. My n00b take: it's...
11
by: puzzlecracker | last post by:
I am not understanding what's happening in the code below (from accelerated c#) -- I know what the outcome but not how .net does it: using System; using System.Linq; public class LambdaTest...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.