Let's create our very first generic class.
I've just created a simple C sharp console application if you want to follow along, which I always
highly recommend, simply create a new console application.
The first thing to do is to create a new class.
So right click on your application, go to add and then add a new class.
We will call this C sharp class box dot CSS and simply hit add.
Now inside of your box you can see that right here we can now specify a so-called type parameter.
So you know it doesn't matter if it's internal or public.
We can leave it as internal.
The class.
Right.
Let me just zoom in.
Go ahead and add angle brackets and then write down T.
As we have already discovered in C, sharp generic means that it's not related to a specific type to
a specific data type.
Right.
So internal class box T.
But what does that t now mean.
Well think of a box just like the package right.
If you have a box which is closed right.
You don't know what's inside.
Inside.
There could be books, there could be fruits, there could be basically anything.
Right.
So let's think of that as t whatever is inside is our T right here which typically stands for type.
So you can also call that type or whatever you like.
But the naming convention says that you should call it t t t value something related to the functionality,
but t typically means type.
We can use that t here, whatever type it is, whatever is inside of our box in our entire class.
By using those angle brackets here, we have now turned our usual C sharp class into a generic class.
All right, now let's start using our T, our generic type inside of our class.
So let's set up a property right down prop hit tab tab.
Now set the type here suggesting an integer.
But we don't want to specify it as a specific data type, not an integer, not another custom class
or custom data type, not the string or whatever we want to set it to t whatever our generic type is,
call the property content the content of the box right?
What is inside the box?
Still, it is generic.
If you do not open the box, could be books, could be fruits, could be string, could be integer.
Whatever.
It is generic.
Great.
Let's create the usual method public string lock to lock the content.
And we simply return add a dollar sign here for interpolation box contains.
And then we want to add curly braces here and then interpolate to the content whatever the content basically
is.
Great.
Now save it like that.
Let's just sum it up.
We have a class which is generic.
The class is called box and it takes a generic parameter t.
Then we have a property which is of that type t.
And in addition we have a method that we can simply call on our generic type which locks whatever is
inside of that box.
Now let's go to our Program.cs so that you can see how to create an instance of your generic class.
Go to program.cs.
And inside here we can now create a new object as usual basically.
But we have to specify a type.
So as you know we have a box.
But our box right here you can see that also has a T.
We need to submit the type in angle brackets.
You can also see that indicated here.
You can see those angle brackets basically everywhere.
And if you're into C-sharp development for quite a while you probably have seen that a couple of times.
Just think about that.
But often you will find those angle brackets and then you just specify a type.
This is basically what a generic does.
So box is generic.
Therefore we add our angle brackets.
When we create an instance.
Then we can specify type for example integer.
Right.
We can do it that way.
No problem.
Box integer.
We call it box.
It's a new instance of our box class and we set the type t to integer.
Now what happens in the background in compile time is that here every t that you can see which is generic
right.
Not related to a specific data type will get replaced for that specific instance here with integer.
So every t box t t content will get replaced with integer for that specific instance.
Now let's go to our box dot content.
Hover above it.
And you can now see that every generic t.
Is gone.
Instead you can now see integer and integer right here, right?
Why is our content now also of type integer?
Well, because our property content here that we have is specified as t and we use or refer t to whatever
is the class T, right.
So if we submit or create an instance for integer, the t used inside of the content property here is
also an integer.
So content is an integer and we can assign one for example.
So let's simply write down c w Console.WriteLine and call box dot log.
Great.
So that's the first part.
Now it is generic.
In this case we have a box which includes numbers integers right.
But we can also create a box again box.
We set the type this time to string.
Now let me tell you it can be any data type right.
It could also be a custom data type like a person you could put an animal into that box or whatsoever.
Right.
But you specify that box.
So this time our box is of data type string.
Let's call it box str equals to new box of type string again.
Now we take our box str which is the second instance of our box right.
Then use it dot content again and take a look at the content.
Now it's of data type string.
For our first box it was of data type integer but now it's of data type string.
This is because we now create an instance of our box using this string data type.
So box str content equals to hello world for example.
Now see w again for Console.WriteLine.
Let's take our box str again and then simply log the contents.
Great.
Now let's save and start our application.
And you will now see that our first box, which we set up with the data type integer, shows the content
is one or the box contains one, and the second instance of our box generic box, which we set up with
a data type string shows hello world!
So both of those instances share the same structure, our box structure, right.
But they both use different data types.
The first one uses integer and the second one uses the string.
So in that way you can create a C sharp class that shares the same functionality across multiple data
types.
But there is so much more to discover on C sharp generics.
So let's move on to the next lesson.