tags:

views:

279

answers:

4

Answers to a recent post (Any chances to imitate times() Ruby method in C#?) use the => operator in the usage examples. What does this operator do? I can't locate it in my C# book, and it is hard to search for symbols like this online. (I couldn't find it.)

+10  A: 

It's not really an operator as such, it's part of the syntax for lambda expressions. In particular => is the bit which separates the parameters from the body of the lambda expression.

Does your book cover C# 3.0? If not, it won't include lambda expressions. If it does, it should really cover them! Hopefully with the right terminology, you'll be able to find it in the TOC or index.

EDIT: A bit more information: A lambda expression is a piece of syntactic sugar to either create an instance of a delegate or an expression tree (the latter being new to .NET 3.5). Lambda expressions almost entirely replace anonymous methods (from C# 2.0) although they don't support the notion of "I don't care about the parameters" in the way that anonymous methods do.

Jon Skeet
Ah, my book does not cover C#3.0, so no wonder I couldn't locate it!
Mr. Mark
+1  A: 

That will be for a lambda expression:

http://msdn.microsoft.com/en-us/library/bb397687.aspx

An example is here:

MyControl.OnMouseDown += (sender, e) =>
{
  // Do something in the mouse down event
};

Here I have created a lambda expression event delegate. It basically saves me from having to create a separate function for it in the class.

Mark Ingram
A: 

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

http://msdn.microsoft.com/en-us/library/bb397687.aspx

Greg Dean
A: 

The => token is called the lambda operator.

It is used in lambda expressions to separate the input variables on the left side from the lambda body on the right side.

MSDN

Rinat Abdullin