What does () => {} mean?

I read by dragging a switch here and stumbled upon this code.

Can someone explain what is () => {}and what should I read to understand this line of code?

var moveMap = new Dictionary<string, Action>()
{
    {"Up", MoveUp},
    {"Down", MoveDown},
    {"Left", MoveLeft},
    {"Right", MoveRight},
    {"Combo", () => { MoveUp(); MoveUp(); MoveDown(); MoveDown(); }}
};

moveMap[move]();
+5
source share
4 answers

This is the lambda expression :

All lambda expressions use the lambda operator =>, which reads "goes". The left side of the lambda operator indicates the input parameters (if any), and the right side contains a block of expressions or operators

Basically, you create a new temporary function here that just calls a combination of the other two functions.

, () , ( ). {} , , " ", , " ", .

+9

. MSDN " ":

lambda lambda, , .

Actions (). 4 , - , 2 . Action ( ).

+4

() => {/*code*/}is a lambda expression, a convenient way to create an anonymous delegate that accepts null parameters. Essentially, it creates the called code snippet, which in your case moves up twice and then moves down twice.

You are not limited to lambdas without parameters - you can create them with arguments:

Action<string> callable = (name) => {Console.WriteLine("Hello, {0}!", s);};
callable("world");
callable("quick brown fox");
+4
source

() it is an anonymous function without parameters

=> is a lambda operator (pronounced Goes to)

The dictionary is initialized by KeyValuePair, the last parameter is an anonymous function that does not accept the parameter and calls other functions

+3
source

All Articles