6

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

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Mr. Mark
  • 69
  • 1
  • 2
  • Does this answer your question? [What does the '=>' syntax in C# mean?](https://stackoverflow.com/questions/290061/what-does-the-syntax-in-c-sharp-mean) – Heretic Monkey Mar 30 '22 at 21:47

4 Answers4

15

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
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Ah, my book does not cover C#3.0, so no wonder I couldn't locate it! – Mr. Mark Oct 07 '08 at 13:56
  • Can I ask you what you mean with `"I don't care about the parameters" in the way that anonymous methods do.`? Thanks! – xanatos Sep 29 '11 at 10:46
  • 1
    @xanatos: For example, to create an `EventHandler` which doesn't use the sender or args, you can use `delegate { Console.WriteLine("Called"); }` without specifying a parameter list at all. That's convertible to *any* delegate type with a return type of void and all "in" parameters. – Jon Skeet Sep 29 '11 at 10:54
3

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
  • 29,221
  • 14
  • 67
  • 78
3

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
  • 23,036
  • 8
  • 57
  • 80
2

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
  • 71,849
  • 51
  • 176
  • 230