What is ‘=>’? (C# Grammar Question)

I was watching a Silverlight tutorial video, and I came across an unfamiliar expression in the example code.

what is => ? what is its name? could you please provide me a link? I couldn’t search for it because they are special characters.

code:

        var ctx = new EventManagerDomainContext();
        ctx.Events.Add(newEvent);
        ctx.SubmitChanges((op) =>
        {
            if (!op.HasError)
            {
                NavigateToEditEvent(newEvent.EventID);
            }
        }, null);

Objective C syntax: what is ->? [duplicate]

Possible Duplicate: What is “->” in Objective C? beginner question here. Im looking through this intro to the objective c runtime (http://mikeash.com/pyblog/friday-qa-2009-03-13-intro-to-the-obje

What does mean /? escape in C grammar? [duplicate]

This question already has an answer here: Why is “/?” an escape sequence in C/C++? 1 answer I was reading this and found the escape /?. What does means exactly this escape? the literal ? inside

What does ()=> mean(c#3.0) [duplicate]

Possible Duplicate: What does () => mean in C#? Hi Everybody, This is my first question in stackoverflow. I have encounter something as () => SomeClass.SomeMethod(param1, param2) This is en

What does “–>” mean in C? [duplicate]

This question already has an answer here: What is the name of the “–>” operator? 18 answers I have a question, what mean –> in C? For example: int a, b, c, x; a=2001; b=1000; c=2; x=a

C# – Basic question: What is ‘?’? [duplicate]

This question already has an answer here: What does “DateTime?” mean in C#? 5 answers I’m wondering what ? means in C# ? I’m seeing things like: DateTime? or int?. I suppose this is specific to

What does `:>` (colon, greater than, aka. smile face) mean in C Programming Language? [duplicate]

This question already has an answer here: What is the significance of the <: syntax in C? [duplicate] 4 answers What is this smiley-with-beard expression: “<:]{%>”? 4 answers recen

“ptr = ptr – > next” What does this “->” mean? (C++) [duplicate]

Possible Duplicate: What can I use instead of the arrow operator, -&gt;? What does -> mean in C++? So I’m currently studying for a C++ exam about Data Structure and Algorithm Development. Whi

What Is => Called and How Is It Used in C# [duplicate]

This question already has an answer here: C# Lambda ( => ) [duplicate] 4 answers I have seen => used in sample code, but I don’t know what this operand is called. Because the term is just

C grammar compound statement [duplicate]

This question already has an answer here: Why was mixing declarations and code forbidden up until C99? 6 answers I was looking at the C grammer on K&R and i found this: compound-statement:

‘>>>’ Java to C++ [duplicate]

Possible Duplicate: What is >>> operation in C++ I need to convert this tiny little part of Java to C + +, but do not know what is ‘>>>’ … searched, but found no references, only



Answers

It’s a lambda expression.

If you’re familiar with anonymous methods from C# 2, lambda expressions are mostly similar but more concise. So the code you’ve got could be written like this with an anonymous method:

var ctx = new EventManagerDomainContext();
ctx.Events.Add(newEvent);
ctx.SubmitChanges(delegate(Operation op)
{
    if (!op.HasError)
    {
        NavigateToEditEvent(newEvent.EventID);
    }
}, null);

Aspects of anonymous methods such as the behaviour of captured variables work the same way for lambda expressions. Lambda expressions and anonymous methods are collectively called anonymous functions.

There are a few differences, however:

  • Lambda expressions can be converted into expression trees as well as delegates.
  • Lambda expressions have a number of shortcuts to make them more concise:

    • If the compiler can infer the parameter types, you don’t need to specify them
    • If the body is a single statement, you don’t need to put it in braces and you can omit the “return” part of a return statement
    • If you have a single parameter with an inferred type, you can miss out the brackets

    Putting these together, you get things like:

    IEnumerable<string> names = people.Select(person => person.Name);
    
  • Lambda expressions don’t support the “I don’t care how many parameters there are” form of anonymous methods, e.g.

    EventHandler x = delegate { Console.WriteLine("I was called"); };
    

I couldn’t search for it because they are special characters.

Sometimes the old-fashioned ways are the best. This worked for me:

  • Start Visual Studio 2008 or later
  • Hit F1
  • Once the Help Document Explorer has come up, ensure the Index tab is selected in the left hand pane
  • Enter => in the Look for field
  • The first item in the list is now the help article you need.