Welcome to this lesson.
We will now take a look on how we can use multiple generic types inside of a class.
So here we got a program.
It's clean.
Here we got a box or a box class internal.
And it's also entirely clean.
It's not even generic yet.
So again we use angle brackets as we did earlier.
If we want to create a generic class.
And now we can define a type right.
We could call it simply T.
But now we don't only want to have one generic type, instead we want to have two.
But before we do that, let's just think about if we have seen that before anywhere, right?
Take a look right here.
If you create a list, for example, a default C sharp list, you write down list and then oh, pay
attention.
You can see that angle brackets here.
Right.
So could it be that a list is a generic type.
And we have to define the type of that list.
Could we say that it's of type string.
Yes we can.
Right.
So let's take a look here at our list.
And you can see it's in the System.collections namespace called generics.
Alrighty.
That's the first part.
So here we define one um generic type string.
But as I said we now want to define two types.
Have we seen that before too.
Well take a look at the dictionary dictionary.
Right.
You can see we have a T key here and a t value.
So we have two generic types.
We can define it as a string comma and integer as an integer integer double float float float string
float whatever.
But the main part is that we have two types here, right?
One for key, one for value.
But it's not about the name or the purpose, it's just that we have two.
We have one generic type and another one.
So two in total.
And again that one is also coming from the System.collections.generic namespace.
So in this lesson we will now take a look.
Let's switch to our boxes how we can do or how we could potentially just create something similar like
a dictionary.
So let's see that in action.
Let's go to our box.
And here we have our generic type.
It's called T.
Let's say it is called t first a comma and then t second right.
So as I've said earlier you can name it however you like.
Just make sure that it makes any kind of sense.
And it's quite short.
That will be great.
Good naming would also be key and value if you are handling it like a dictionary, we will do that,
but I want to make a clear separation from the dictionary so that you can see you can build it on your
own without referring to key and values and whatever.
Let's create two properties here prop hit tab, then write down for the first type it's T first our
generic first type from our class.
And we can also call it first here.
Then create another prop tab T second name second.
So we've already used our two generic types inside of our class.
Let's create a constructor right down seater hit tab take T first first and secondly t second second
for initialization first is equals to first right and second is equals to second.
Now finally let's create a method here public void display right down CW dollar symbol for interpolation.
First is equals to first and second is second.
Great.
You have already seen how to use your types that you have defined inside of your class, inside of methods,
constructors, fields and so on.
Right?
So this lesson is only about showing you that you are not restricted and limited to one type.
You can use multiple types T first and t second.
We're using them right?
We are initializing them inside of our constructor.
And now we have a simple logic which is just referring to them for display.
Let's go to our Program.cs.
Let's create a new box.
This time t first would be integer, second would be uh, let's say string.
Let's call it box, let's initialize it new box.
And this time you can see it's of type int string.
And we can initialize it because we have created a constructor.
So let's say the initial value is 100.
And the second value is a string where we can simply say like 100.
You can do it that way, right.
Whatever you want to do, it's just more about the logic right here.
Cool.
So finally take our box and call display.
Save it like this.
Start the application and you will be able to see that you have now created an instance of an object
which is generic and uses two dynamic types, the first one as an integer and the second one as a string
initialized.
Created and printed out to the console.
And now you have gained the understanding of how data types like lists, for example, and dictionaries
are built in C sharp right under the hood.
And still, there's so much more to discover on generics.
So let's move on to the next lesson.