Hello.
In this lecture, we'll talk about Question seven.
What are delegates in C-sharp?
We can declare several kinds of types.
We can declare a class or a struct to represent data and methods.
We can declare in arms to represent some predefined discrete values, and we can also declare delegates.
A delegate is a type whose instances hold a reference to a method which particular parameters and return
type.
Let's define a simple delegate.
We declared delegates with the delegates keyword similarity as we define classes with the class, keyword
or structs, we've this tracked keyword.
Besides the delegates keyword, a delegated declaration looks the same as a method of declaration.
First, the return type, then the name and finally, the list of parameters.
All right.
We defined our delegate type and and the variable of this type can be assigned a method that takes us
three as parameter and also returns a string, for example.
Both those functions can be assigned to the variable of the process string delegate type.
We can now call the delegates and as a result, execute the functions that are stalking them.
As you can see, the methods start in, the delegate variables have been invoked.
The interesting feature of delegates is that we can store more than one function in one variable of
the gate type.
As you can see there, multicast variable holds references to two functions, not one.
Let's see what will be the results printed to the council.
As you can see, both functions stalled in the delegate, Baraboo has been executed.
We can also use the plus equals operator to train another method to delegate variable.
Now, free methods has been invoked a decade Baraboo, that holds references to more than one function.
It's called Multicast Gallowgate.
All right.
In the previous lecture, we learned about funk and action types.
What is interesting?
Both are simply generic delegates.
For example, this is the definition of the funk that takes two parameters.
This delegate represents a function taking two parameters of any kind and returning some result.
So exactly what a funk with two parameters can be present.
Of course, instead of a lambda expression, we can also assign a pre-existing function to a variable
of a funk, though, as you can do with any delegate.
During the interview, you might be asked this question, what is the difference between a funk and
another great well?
Funk is a delegate simply defined by Microsoft, not us, to be more precise.
Funk is a generic Gallowgate used to represent any function with given parameters, and we can't type
a delegate is a broader concept than funk.
We can define any delegate we want, and it doesn't need to be generic at all.
All right.
So we know that funks and actions are generic delegates.
Because of that, we can represent any function we want with funk or actions, right?
Let's see again the delegate variables we declared at the beginning of this lecture.
We don't really need to define process string delegate.
We could use the funk instead.
So why bother declaring delegates if we can use Funk's instead?
Well, first of all, delegates existed in C-sharp before funks actions and lambda expressions.
There was a time when they were simply the only way to represent functions as objects.
So you can still see some old gold full of custom delegates.
Instead, the funks and actions for the simple reason that it was created a long time ago.
Nowadays, from my practice, I would say that in the 99 percent of the cases, there is no point in
declaring custom delegates and funks can be used instead.
Personally, I almost never use other delegates than funks and actions, but there are some scenarios
when custom non-genetic delegates can be useful.
First of all, some people prefer world named dugouts to slightly cryptic funks in this interface.
We have two methods doing the same thing, but one takes a funk and the other a custom delegate run
command is definitely more human readable than funk of common type and bool.
Some people think such a named delegate is better.
Personally, I disagree for a simple reason.
When I look at the funk of common type and bull, I know that the input to dysfunction is common type.
And they also know that if returns a ball and they can make an educated guess that this is probably
a boolean telling me if the command execution was successful or not.
We've run command.
I don't have this information and I need to go to run command, to type, to see what actually are the
parameters and return type.
Also, we funk.
I can see right off the bat that this is an executable method.
Run command could be some ordinary object as well, and they won't know that unless I go to this type
and see the Gallowgate keyword, but I want to underline that this is a matter of personal taste.
If for you and your team custom delegates are more readable, you should definitely use them everywhere
you want.
Another use case when custom delegates can have an advantage over Funk's or actions is when a specific
delegate is used or around the application.
This way, instead of declaring sometimes complex func everywhere, I can have a single declaration
of a custom delegate.
Here are defined calculate on nuance, and I can use it everywhere in the product we func.
I would need to write this pretty long definition everywhere when I wanted to use it.
Also, there are some use cases when we simply must use custom delegates instead of funks or actions.
One of the examples is when we want to use them with methods with rough or out parameters.
It's not possible using Funk's.
As you can see, having an outer perimeter doesn't work with funk, but it works fine with custom delegate
S.M.A.R.T funks countless optional parameters while custom delegates can.
Again, funk doesn't compile.
But, Doug, it works fine.
Also, we can't have funks with the params parameters.
As you can see, sometimes a custom delegate is simply the only choice we have.
Let's summarize a delegate is a type whose instances hold a reference to a method with particular parameters
and return type in C-sharp.
We can use the predefined generic delegates funk and action to represent any function we want.
During the interview, you might be asked What is a multi-course delegate?
A multi custom delegate is a delegate holding references to more than one function.
All right, that's it.
For this lecture, we will revisit the topic of delegates in the lecture about events as using their
gates is crucial when working with them.
Thanks for watching this video, and I'll see you in the next one.