I was reading Pulling the switch here and came across this code.
Can somoone please explain what is () => {} and what should I read up to understand that line of code?
var moveMap = new Dictionary<string, Action>()
{
{"Up", MoveUp},
{"Down", MoveDown},
{"Left", MoveLeft},
{"Right", MoveRight},
{"Combo", () => { MoveUp(); MoveUp(); MoveDown(); MoveDown(); }}
};
moveMap[move]();
It’s a lambda expression:
Basically you are constructing a new, temporary function here that just calls a combination of two of the other functions.
As seen above, the
()on the left side means that it has an empty parameter list (just like your other functions). The{}on the right means that it executes several statements inside a block, which makes it a “statement lambda” that is called for its side effects, in contrast to an “expression lambda”, which computes a value.