views:

529

answers:

7

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.

I'm a vb.net guy trying to do more in C#. I already tried Google and some of my reference books. Nothing.

+1  A: 

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

tvanfosson
+10  A: 

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);
Hamish Smith
I never noticed how confusing it is when you stick lambda with 'less than or equal'. It ends up looking like all arrows pointing towards i.
Will Eddins
+5  A: 

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.

Rex M
+3  A: 

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

David in Dakota
+1  A: 

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.

mquander
Technically, the whole body (int p) => p *2 is a lambda expression. The => is just the lambda operator.
Richard Hein
+2  A: 

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.

womp
You can also read it as "becomes" or "maps to"
Joel Coehoorn
I've heard "injects", "arrows", something vulgar I won't mention here, and a couple others. My favorite so far is "equals greaterthan'd".
womp
I just say "lambda" now, instead of worrying about the countless possible variations.
Richard Hein
+5  A: 

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
Richard Hein
I get voted down again for no reason. The guy who asked the question said he's a VB.NET guy, so offering an explanation in terms of VB.NET makes sense.
Richard Hein
You didn't answer his specific question, but I don't see why giving him a piece of information that might help him relate better would warrant a downvote. +1 to negate it.
Brandon
+1, I was about to add this answer too... Crazy that someone would downvote it, it's very useful to know for a VB.NET guy...
Meta-Knight
Well thanks for negating my downvote guys. I didn't want to repeat earlier answers explaining what it was called, but next time I will answer the specific question first, and then add whatever additional information might help.
Richard Hein
+1 For solidarity - uncommented downvotes are unhelpful and pointless IMO
Gordon Mackie JoanMiro