Let's talk about how to use generic types in methods only.
So as of right now, if you're following along with that section, you know that you can create generic
types for classes, but you can also do this only for methods.
Let's take a look by an example.
Create a new C sharp class.
Let's just add a new class here.
Let's call it logger dot c s.
So internal class logger.
As I said, if we would like to create a generic class we would now use in the class declaration the
angle brackets as you have already seen.
But we don't want to do that.
We only want to have one method use a generic type.
So let's create a public void right?
Let's call it log.
And we want to use inside of our parameters a generic type right.
So we want to log whatever the type is.
So go ahead and follow the name of our method.
Add angle brackets again say that this is using a generic type called t again.
Then open up the parentheses for our parameters.
And now we can for example say t message.
And he can now refer to whatever t is.
Let me just show you the difference if I copy that or if I don't have to copy that.
If I just remove the angle brackets and the T, we will now see an error because he does not know what
T refers to.
He doesn't know what the generic type then is.
So we have to use the angle brackets for our method to define.
Hey, in that method I want to use or I want to refer to that generic type.
So now we have a generic method inside of a default C sharp class.
Because our class is not using a generic type only our method here is generic.
Usually you want to decide when to do that right depending on the functionality.
So if only a small part of our logger functionality, for example, has to be generic, our entire class
doesn't have to be generic.
It would be enough just to make one method generic, as we did right here on the other side.
If we would have a box, right?
And that box contains generic content and we need that type, that generic type in all of our methods
and fields and properties and constructor and so on.
Then you would definitely go ahead and use a generic class over a generic method only.
Right?
Cool.
So let's complete the functionality here.
Let's simply see w Console.WriteLine.
And we simply take our message dot two string.
There we go.
Then save it.
Like this.
Let's see that in action.
Let's go to our Program.cs.
And as I said usually if I would create a logger then we would have a static class or whatever.
But we can now simply create a logger instance.
So let's call it logger equals to new logger and all of that.
We are not specifying any generic type.
Right.
Because our class is not generic.
But when we now want to call logger let's call logger dot log.
You can now see that we have a generic method right only here you have already seen that but not in
that kind of context.
Right.
So Logger.log now let's provide a data type.
For example we could say uh integer.
Right.
Add parentheses here.
Let's say ten.
For example let's create another logger log call not a logger instance.
Right.
Because we don't need a new instance because only in the method we are defining the type, we can refer
to that logger instance.
Still, let's say string can say hello world or whatever.
Right.
So one instance this time because the generic type is only, um, well needed inside of our log right
here, you can also see that we can reduce the amount of code.
We can remove the, uh, the type definition right here for our generic type.
Right.
But I wanted to make it obvious.
So the C sharp is clever enough to automatically recognize what type we are sending in.
And that log here is a generic method.
But as I said, I want to make it as obvious as possible so we can specify our generic type right here.
Cool.
So if we save it we should be able to see some console, uh, entries.
Let's just start the application.
As you can see we got ten and hello world here.
So that worked fine.
And the great thing about it is that we don't even as I said, we don't have to specify data type here,
but we can even simply pass an object.
So logger log.
Right.
And then we're not referring to a string or to an integer.
We simply return an object.
Let's create a new object here.
Let's set for example the name to John.
And let's say it, the age to for example, 30.
So here we have a very simple C sharp object.
And if I hit start again we'll be able to also see that entry.
You can see name John age 30.
He was able to do that because our lock method is generic.
And we can simply pass in any kind of C sharp object.
And he will be able to print that out because it's generic, right?
It's not explicitly bound to a specific data type.
And even if we create like an object in an inline style like that still works fine because generic can
handle that.
All right.
So that's how you create a generic method inside of a usual and default C sharp class.
Now let's move on to the next lesson.