Lambda Expressions Flashcards

1
Q

What is a lambda expression?

A

A lambda expression is an anonymous function that you can use to create delegates or expression tree types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Before lambda expressions, how methods were called?

A

They were called as named methods (regular methods). Or they would be called as delegate methods (noisy syntax).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Hows does this (deprecated?) anonymous delegated method called?

A

linq cities.where(delegate(string s) {return s.StartsWith(“L”); });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Hows does a lambda expression look like?

A

linq cities.where(s => s.StartsWith(“L”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Is it really necessary to have lambda expression with one letter params (e.g e.Name)?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How does visual studio show in auto-complete, that the method you’re calling receives a lambda expression?

A

(Func predicate)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How many parameters a lambda expression can have?

A

Up to 17.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does a func type look like?

A

Func

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Hows the compiler now which param is the return?

A

Is always the last one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to declare a func and create a lambda expression that implements it?

A

Func square = x => x * x;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When is it necessary to have a parenthesis in the lambda expression?

A

When the Func expression takes 0 or more than one parameters. e.g: Func add = (x, y) => x+ y;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to declare a lambda expression that returns void?

A

It is called Action and is declared as: Action write = x => Console.WriteLine(x);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly