13

I am looking for a very simple - basic - no hardcore programming mumbo jumbo, simply put a generalized overview of a Lambda Expression in layman's terms.

4 Answers 4

9

A lambda expression is, simply put, a re-useable expression which takes a number of arguments:

x => x + 1;

The above expression reads "for a given x, return x + 1".

In .NET, this is powerful, because it can be compiled into an anonymous delegate, a nameless function you can declare inline with your code and evaluate to get a value:

int number = 100;

Func<int, int> increment = x => x + 1;

number = increment(number); // Calls the delegate expression above.

However, the real power of a lambda expression is that it can be used to initialize an in-memory representation of the expression itself.

Expression<Func<int, int>> incrementExpression = x => x + 1;

This means that you can give that expression to something like LINQ to SQL and it can understand what the expression means, translating it into a SQL statement that has the same meaning. This is where lambdas are very different from normal methods and delegates, and normally where the confusion begins.

Sign up to request clarification or add additional context in comments.

Comments

3

Lambda Expressions are inline functions that have a different syntax to regular functions.

Example Lambda Expression for squaring a number.

 x => x * x

Comments

2

A small unnamed inline method. Is that basic enough for you? I'm not sure what you are looking for exactly.

You also said in "layman's" terms - I presume that you have some level of software development experience (so not a complete layman)

1 Comment

If you look at the code via something like Reflector then it is a method. I suppose you can, like VB does, sub categorise methods into Subroutines (no return value) and Functions (returns a value). However lambdas do not have to return values. For example, lambdas used with Parallel.For do not return values because they map to Action<...> rather than Func<...>
1

In non functional programming languages expressions (that act on variables) perform calculations and they perform those calculations once.

Lambda expressions allow you to define (in an expression) via different syntax code that can work on a list and can conceptually be considered a function.


You could simplify this to say "They let you define functions in expressions".


Does not quite get to the "why". The why is the more interesting, in my opinion. Lambda expression allow for the manipulation of functions and partial functions.

1 Comment

"You could simplify this to say "They let you define functions in expressions"." That's what I was look for!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.