You have learned a lot about generics already, right?
You can now create generic classes, interfaces, methods, you can combine all of them, and so on.
There are some built in generic delegates in C sharp that we should talk about.
I'm talking about action and func for example.
This is definitely something that you will see during your C sharp development career.
So this is why it's covered here.
We've already talked about delegates in a previous section of this course.
And in this video we are talking about the action generic delegate.
So you can see right here we can call action.
Then you can see the generic well angle brackets that we're using all the time.
On the right side you can see that this is a delegate void.
Right.
And let's just call it this is best explained by example.
At least that's what I think.
So here we have an action.
Let's say this action does not take any parameter.
We just want to write down a console log.
So action.
Let's say that this is called action.
And the value of this one is just we will create basically an arrow function here.
Now right.
We have no parameters.
And we will execute a simple console right line.
So you could say c w Console.WriteLine.
Say hello world.
If you have never seen something like that before let me just explain it to you in easy words.
This part basically here is very similar to a method, right?
However, a method has to return something.
An action has no return value.
We can just execute some code.
In our case we have no parameters.
This is why we have empty parentheses here.
Then we have the arrow because we say hey, take the parentheses or parameters if we have some.
And then what you want to do is you want to execute whatever is inside of the curly braces.
So we want to console write line.
You know we can do that in multiple lines for sure.
So we could just copy that over and write down something here because we have wrapped that around with
curly braces.
All right.
So we're not limited to one line or whatever.
We can write it down just like that.
So we can now call this action by writing down action.
We're calling it like a method.
Right.
So we now have that action set up.
And if we start our application you will be able to see Console.WriteLine.
Hello world.
Let's just give it a second to compile.
And then you will see that, you know, the part about actions is that you can submit multiple parameters.
We'll get to that.
You can see hello world.
So that was fine right okay.
So what makes it generic.
Well the generic part begins when we say that we create another action and this time it is of type integer.
For example let's call it num print goes to.
And now we can add parentheses here and create a name for our first parameter or argument.
Let's call it x or number or whatever.
And then x goes to.
Then we have curly braces here Console.WriteLine x.
Add a semicolon here and we're good to go.
Cool.
We just add that curly brace here that got removed.
And if you only have one parameter then you can remove the parentheses.
Just like that kind of looks like a lambda expression or whatever.
Now let's call our num print action.
Let's submit ten here.
Right.
Don't forget it is of type integer.
So we have to submit an integer right.
Let's hit start.
You will be able to see Hello World and ten should work fine.
Out of the box.
Hello world and ten.
There we go.
Now let's move on and let's create another action.
This time let's do it of type float.
And the cool thing about actions is that we can submit up to 16 parameters.
So we could basically add 16 parameters as floats here.
Right.
So we don't want to do that.
Or we could say we have a float float and float.
We could say we have a float a string and a float.
Right.
We could just do it however we like.
For the sake of this video.
We want to do it as float, float, float.
But it's generic, right?
You can add as many generic parameters as you like.
Doesn't have to be float float float could be int float, double, could be string, person, shoe or
food or whatever.
It doesn't really matter.
You can just submit whatever you need.
Let's call it again, just as some let's just call it some.
And it goes to this time we just say it's x, y and z.
We have three arguments, right?
Three parameters because we defined the action as using three generic types here float float and float
goes to.
Now we can simply write down Console.WriteLine x plus y plus z.
Or we can use any library that offers us some functionality.
Right?
We don't care about that.
It's more about the action itself.
And in all of that cases we never return anything.
We're just executing that.
Console.WriteLine.
All right.
Let's hit start.
You should get the idea of a generic action right.
Um, we just have to let me take a look for sure.
Call that.
So let's call some.
Let's call one, two, three.
That should result in six.
So let's see that in action.
There we go.
It's showing six.
Perfect.
All right so that's the action.
As I said an action does not return a value already.
We just create the function kind of functional programming right.
We say hey we have created a function called sum.
It takes in three generic arguments for this action.
We specify them as float, float float.
Here we give a name to them right x, y and z which we can refer to.
And then we have anything that gets just invoked, just executed.
Right.
Code that just runs.
You can see here if you hover above that the type one is a float, T2 is a float, T3 is a float.
And you know, you know, the part here is that this is kind of simple.
What you can see here is simple in comparison to what we have discovered before in the generic section.
Right.
But I tell you that if you have no clue about generics, but you want to use an action, or you want
to use a func, which we will talk about next, you're pretty much lost.
If you like, you can use it, but you don't understand what's going on under the hood, right?
This is why we have discovered generics in C sharp so much in depth.
So you can now hover above action and you can clearly see what's going on.
You know what all of the types are and so on.
So this is how you can create generic actions in C sharp.
Let's move on to the next lesson.