This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Lamda Explanation and what it is as well as a good example
What is the => token called?
I have seen this code:
myContext.SomeEntities.Single(x => x.code == code);
And I don´t know what does the => operator do.
Every search on google about the operator returns no results.
Thank you.
The => operator designates a Lambda Expression:
A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.
All lambda expressions use the lambda operator =>, which is read as "goes to". The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read "x goes to x times x." This expression can be assigned to a delegate type as follows:
static void Main(string[] args)
{
Func<int, int> func = x => x * x;
int j = func(5);
// j == 25
}
Lambda expressions, very cool.
http://msdn.microsoft.com/en-us/library/bb397687.aspx
This is defining a lambda. You can read it "x goes to x.code equals code," and it means that given x, return the result of the given comparison.
It signals that the code is a lambda expression.
More info:
http://msdn.microsoft.com/en-us/library/bb397687.aspx
They are related to lambda expressions.
You can read about Lambda Expressions here:
http://www.rvenables.com/2009/03/practical-introduction-to-lambda-expressions/
Related
I am trying to understand how enumerables work in C# (and also how they work with lambdas) and have the following code that I was playing with.
bool[] boolArray = new bool[100];
boolArray = boolArray.Select(x => x != null).ToArray();
I understand Enumerables.Select will return an IEnumerable<bool> but how does the lambda expression, under the hood, act upon each element in the array? From my understanding the x on the left is the parameter which "goes to" the expression to the right of the => operator but it still doesn't feel like it should be enumerating.
What is the difference between a lambda expression and a predicate in .NET?
A predicate is delegate (function object) that returns a boolean value. Lambda expressions can be used to define any anonymous function, which includes predicates, e.g. to express a predicate in the form of a lambda expression:
Predicate<int> isEven2 = x => x % 2 == 0;
which is functionally equivalent to:
Func<int,bool> isEven = x => x % 2 == 0;
Predicate defines a set of criteria, while lambda expression is an anonymous function. You can use lambda ex. as a predicate, but that doesn't mean they are the same thing.
Predicate
Lambda expression
Can you say what is the use of the ()=> and =>? I saw this in a code. I did not get any reference for this.
this.Dispatcher.BeginInvoke(()=>
{
//some thing..
};
=> is the lambda operator in C# and is read as "goes to". A lambda expression is an anonymous function and can be used to create a delegate.
Your example takes no arguments as indicated by the empty parens preceding the lambda operator. A lambda expression with one argument might look like this:
n => n.toString()
That expression would return the string representation of n, when invoked. A lambda expression can have multiple arguments as well, contained in parentheses:
(n, f) => n.toString(f)
A common use would be in a Func<T>:
Func<int, string> getString = n => n.toString();
int num = 7;
string numString = getString(num);
This is, of course, a silly example, but hopefully helps to illustrate its use.
This notation is that of a lambda expression which takes no argument. If the lambda expression made use of arguments they would be declared in the empty set of parenthesis as in say...
this.Dispatcher.BeginInvoke((x, y) => { do some' with x and/or y }, 12, somevar);
In a nutshell, lambda expressions allows creating "nameless" functions, right where they are needed.
In the example of the question, the BeginInvoke() method requires its first parameter to be a delegate (a "pointer to a method"), which is exactly what this lambda expression provides.
It's a lambda expression that has no parameters.
Check out this page http://codebetter.com/karlseguin/2008/11/27/back-to-basics-delegates-anonymous-methods-and-lambda-expressions/
If you don’t have any parameters, like in our example, you use empty
paranthesis:
() => {…}
I will keep it really simple,
How do I get expression tree out of lambda??
or from query expression ?
You must assign the lambda to a different type:
// Gives you a delegate:
Func<int, int> f = x => x * 2;
// Gives you an expression tree:
Expression<Func<int, int>> g = x => x * 2;
The same goes for method arguments. However, once you've assigned such a lambda expression to a Func<> type, you can't get the expression tree back.
Konrad's reply is exact. You need to assign the lambda expression to Expression<Func<...>> in order for the compiler to generate the expression tree. If you get a lambda as a Func<...>, Action<...> or other delegate type, all you have is a bunch of IL instructions.
If you really need to be able to convert an IL-compiled lambda back into an expression tree, you'd have to decompile it (e.g. do what Lutz Roeder's Reflector tool does). I'd suggest having a look at the Cecil library, which provides advanced IL manipulation support and could save you quite some time.
Just to expand on Konrad's answer, and to correct Pierre, you can still generate an Expression from an IL-compiled lambda, though it's not terribly elegant. Augmenting Konrad's example:
// Gives you a lambda:
Func<int, int> f = x => x * 2;
// Gives you an expression tree:
Expression<Func<int, int>> g = x => f(x);
Forgive me if this screams newbie but what does => mean in C#? I was at a presentation last week and this operator (I think) was used in the context of ORM. I wasn't really paying attention to the specifics of syntax until I went back to my notes.
In C# the lambda operator is written "=>" (usually pronounced "goes to" when read aloud). It means that the arguments on the left are passed into the code block (lambda function / anonymous delegate) on the right.
So if you have a Func or Action (or any of their cousins with more type parameters) then you can assign a lambda expression to them rather than needing to instantiate a delegate or have a separate method for the deferred processing:
//creates a Func that can be called later
Func<int,bool> f = i => i <= 10;
//calls the function with 12 substituted as the parameter
bool ret = f(12);
Since nobody mentioned it yet, in VB.NET you'd use the function keyword instead of =>, like so:
dim func = function() true
'or
dim func1 = function(x, y) x + y
dim result = func() ' result is True
dim result1 = func1(5, 2) ' result is 7
It's shorthand for declaring a lambda.
i => i++
is (sort of) the same as writing:
delegate(int i)
{
i++;
}
In the context of:
void DoSomething(Action<int> doSomething)
{
doSomething(1);
}
DoSomething(delegate(int i) { i++; });
//declares an anonymous method
//and passes it to DoSomething
which is (sort of) the same as writing:
void increment(int i)
{
i++;
}
Just without giving it a name, it allows you to declare a function in-line, known as an "anonymous" function.
When said aloud the operator is the lambda (goes to) operator which helps to define the anonymous delegate that you're defining in the lambda.
A common place to see this is with an event handler. You will often have a a page load type event that is handled by a lambda with the following code:
this.Loaded += (o, e) => {
// code
}
You've defined a method handling the Loaded event anonymously (it doesn't have a name) by using a lambda expression. It would read as "o, e goes to ... method definition with foo."
This is the "lambda operator", and you read it as "goes to". Say you had the statement:
doSomething(x => x + " hi");
You can replace the "=>" in your mind with this:
doSomething(delegate (string x) { return x + " hi" });
As you can see, it offers a heck of a shorthand. The compiler figures out the type of the variable that you're passing, and allows you to get rid of the function signature and bracketing for the code that you're passing the signature variables into.
It's a lambda operator, part of a lambda expression.
All lambda expressions use the lambda
operator =>, which is read as "goes
to". The left side of the lambda
operator specifies the input
parameters (if any) and the right side
holds the expression or statement
block. The lambda expression x => x *
x is read "x goes to x times x."
It's syntax to declare an anonymous function, known in C# as a "lambda expression."
For example, (int p) => p * 2 represents a function that takes an integer and multiplies it by two.