Lambda Expressions Flashcards
What is a lambda expression?
A lambda expression is an anonymous function that you can use to create delegates or expression tree types.
Before lambda expressions, how methods were called?
They were called as named methods (regular methods). Or they would be called as delegate methods (noisy syntax).
Hows does this (deprecated?) anonymous delegated method called?
linq cities.where(delegate(string s) {return s.StartsWith(“L”); });
Hows does a lambda expression look like?
linq cities.where(s => s.StartsWith(“L”));
Is it really necessary to have lambda expression with one letter params (e.g e.Name)?
No… you can have full names, such as employee => e.Name. But as the whole intention of lambda expressions is to save up time and space this became very common.
How does visual studio show in auto-complete, that the method you’re calling receives a lambda expression?
(Func predicate)
How many parameters a lambda expression can have?
Up to 17.
How does a func type look like?
Func
Hows the compiler now which param is the return?
Is always the last one
How to declare a func and create a lambda expression that implements it?
Func square = x => x * x;
When is it necessary to have a parenthesis in the lambda expression?
When the Func expression takes 0 or more than one parameters. e.g: Func add = (x, y) => x+ y;
What happens when the lambda expressions is kinda big and needs more than one line of code to implement it? What is necessary to be done then?
You need to add a pair of curly braces and then it is necessary to define a return keyword. e.g: func add => (x, y) => {
int temp = x+y;
return temp;
};
How to declare a lambda expression that returns void?
It is called Action and is declared as: Action write = x => Console.WriteLine(x);