Hello.
In this lecture, we're going to talk about Question six, what are Funk's and lambda expressions?
When thinking about objects, we usually think about things that carry some data as the payload, as
well as the operations that can be executed on this data.
An integer holds the value of five a person object, Holt's name, last name and year of birth, and
perhaps a method calculating the age.
We can easily understand that we can have a variable of such types, or that we can pass objects of
those types as parameters.
But could we have variables holding functions?
Can we pass a function as a parameter?
As it turns out, we can, and it's extremely useful.
Let me show you one example I want to write a method that works if any number in our collection is larger
than 10.
This is how I could do it.
All right.
This method is pretty simple.
It's just it allows the collection, and for each number, it's jokes.
If it's larger than 10, in this case, it returns true.
Otherwise, false will be returned.
But soon after, I am asked to add another method that checks if any number in the collection is given.
Let's create such a method.
The problem is that this method is almost the same as the previous one.
The only place in which they differ is this.
This is the part that differs.
So if I wanted to refactor those two methods into one, I would need to make this part a parameter.
But what is this part exactly?
At first glance, you may think it's a Boolean, but that's not correct.
I couldn't pass this Boolean to those methods, as it may differ for each of the elements of the numbers
collection.
So it's not a Boyan, it's a function that takes a number and returns a Boolean.
If I wanted to refactor those methods and make them a single method, it would need to take a function
as a parameter.
And for this, we can use the flag type.
Let me show you how it looks.
This fund can be assigned and the function that is taking a number and returning Abu, as you can see
fund is generic and by type parameters, we define what is the return type of the function and what
parameters it takes.
The last type parameter is always the return type and everything preceding it.
Are the types of parameters.
For example, this Baraboo can be assigned and the function that takes an end date, time and string
parameters and returns other similar for voice functions.
We must use action type.
This variable can represent and the vote function, taking two strings and a ball.
Let's go back to refactoring.
I want to have a single is animated, taking a collection of numbers and the function that defines the
predicate that will be tracked.
Now, instead of using number is larger than 10 or number is even, I will simply use the predicate.
As you can see, we can call the funk object like another method.
Let's use this method now.
First of all, I will define is larger than 10 and is even methods.
Now I can use those methods as parameters of that is animated.
As you can see, we can simply assign methods to the variables of funk type.
Naturally, it wouldn't compile if the signatures of the methods would not match the signatures of funks.
I can, of course, keep declaring variables and simply pass the methods as parameters.
Let's make sure this code works.
Right through has been treated twice because this number is larger than 10.
And this is even.
Right.
We learned that in C-sharp, we can treat functions like other objects.
We can assign them to variables or pass them as parameters.
Funk and action are the types that allow us to do this.
Now let's move on to the ramped up expressions before I explain what they are exactly.
Let's take another look at those two methods.
Those methods are quite specific.
I'm not likely to use them anywhere else.
They needed to declare them in this class so I can pass them as parameters.
Imagine you have a class that does plenty of a very specific tax on collections, like checking if any
number is divisible by five.
If the string is longer than 10 letters, or if any person is named John, we would clutter our class
with tiny, simple methods that took those feelings and are not used anywhere else.
That's not a good approach.
It would be better if we could have an easy, short way of defining small, simple and specific functions.
And that's exactly what the expressions are for.
First, let me show you how they look, and then I will explain in detail.
As you can see now, I don't use the is larger than ten and even the methods, I can safely remove them.
I defined those jokes in form of lambda expressions, a lambda expression is a special way of defining
anonymous function.
This is the general pattern of any lambda expression on the left side of the arrow.
We declare a parameter or parameters of the function on the right side.
We declare the expression whose result will be returned from this function.
We can also have lambda expressions with more than one parameter.
In this case, we must put parameters in parentheses.
This is much shorter than the traditional way of declaring functions.
There is no return keyword, as it is simply assumed that the result is calculated on the right side
is returned.
The result type is not declared because it is inferred from what the expression evaluates to.
For example, in this lambda expression, it is obvious that the return type of dysfunction is string.
The types of parameters are also not formally defined.
So how does the compiler know what they are?
Well, it also infers them from the context.
Let's see this code again.
That is, the method expects a very particular function as the parameter, a function that takes a number
and returns a ball.
Because you use the lambda expression as the parameter of the animated, the CP. infers that the parameter
of the lambda expression must be an integer because that is the method.
Expect such a function as the parameter.
This is also the reason why we can't assign lambda expressions to implicitly type two variables.
As you can see, this doesn't work because there is no context from which the compiler can infer the
type of the parameter.
You may say that lambda expressions require a lot of assumptions and inferring.
But see how little code we needed to write to declare a function checking if a number is given.
And again, this is how this function looked like when we declared it in a traditional way.
Lambda expressions are great when declaring short, specific functions that will most likely not be
used in another context.
For example, they are extremely often used when working with.
Actually, that is a method we declared is almost identical to the animated from being.
Let's summarize in C-sharp we can treat functions like any of our types, assign them to variables or
post them as parameters to other functions.
The funk and action types allow us to represent functions.
Lambda expressions are a special way of declaring anonymous functions.
They allow us to define functions in a concise way and our most useful when those functions will not
be used in different contexts.
When talking with the interviewer, you may have questions like what is the signature of a function
that could be assigned to a variable of this type?
It would be a function that takes two integers as parameters and returns.
Abu What is an action?
Action is a type used to represent voiced functions.
It works similarly to func, but func can only represent non-votes functions.
All right, that's it for this lecture.
Thanks for watching, and I'll see you in the next one.