In this lesson, you will learn how to use your generic type from your class inside of its methods.
So here we got our program CSS.
As you can see it's clean.
And then we have a class box.
It's an internal class box with a type of T right.
So this is a generic class box type of t.
Now let's create a private t content here.
So we have a field which is private of type T which is what we define here in our declaration of our
class.
So we cannot access the content from outside.
So we will have to make use of methods that can actually then add information to the box.
So let's say adding to the box, deleting from the box and getting information from that box.
Right.
Let's create a constructor right down CTR.
Hit tab.
So public box.
Now we can set an initial value of type T right.
So initial value.
Then we go to our content.
And our content is equals to the initial value.
So in our constructor we are now referring to the type that we are using inside of our declaration.
So when I initialize or create an instance of that generic box with the type of integer, then for sure
in our constructor type T will also become an integer.
Great.
So now let's create three methods which are basically also using the same type inside of the parameters.
Let's create a public void update content.
It takes again T right.
And then we can say new content for example as a parameter name.
And here we can say that our content is equals to new content c w Console.WriteLine.
And then we can say with a dollar symbol here updated content to.
And then we can write down our content.
That will be the first method.
And as you can see again we are using the type right.
So we declare it inside of our class and we can use it basically everywhere.
So right now there is still no hard coded data type, right?
Even if we now update the content and run a method, whatever we want to do here and we refer to the
new content, that parameter is again a generic type.
So even inside of our update content method here, when we use new content we are still referring to
that generic type.
Great.
So everything is highly modular as of right now.
The same works for the return type of a method.
So let's create a public t we are returning, not a specific data type.
We are not returning an int or whatever.
We are returning t, right?
Whatever our generic class type is.
So public t get content, no parameters right here.
And then we can simply return content.
And that works fine because content is a generic type T and we are returning type T again.
Let's go to our program CSS.
Let's save the changes here.
Go to the program CSS.
Let's create a new box.
So let's say box of type string.
For example box str we'll call it that way equals to new box of type string.
We have to initialize the value because we are using a constructor here.
Public box right is our constructor with a type initial value.
You can now see if I take a look here and hover above that.
Our box is initialized with a data type string.
And we also have to use that string inside of the parameter for the initialization value.
So let's say hello world works fine out of the box.
Now we can use our box str dot.
We will now have access to our methods get content.
For example it's now of type string right.
So we are returning a string from there.
And for update content we will have to pass a string to update the content.
Right?
So let's update the content.
Choose update content here.
Let's say teaching C sharp is fun right?
And then let's box str dot get content right.
And we can wrap that inside of a Console.WriteLine.
So Console.WriteLine.
And we will wrap that put it just inside that parentheses here.
Cool.
So if we save it and run that we should be able to see that the content got changed to teaching C sharp
is fun.
And then we'll get printed again from our Get Content method.
You know it just shows that we can create methods and use the generic type of our class as a parameter.
But also we can return that type from a method.
Right.
So let's take a look here.
Once it got loaded you can see updated content to teaching C sharp is fun.
And then again it prints out the value.
Teaching C sharp is fun, so it works fine out of the box.
And.
Well, we can.
As you have already seen in the previous lecture, we can for sure.
Now create again another box of type integer, for example, and repeat this the same.
So it doesn't matter if it's like a string or integer.
That's what generic means basically.
Right?
But the key part of this lesson was to show you that you can use your type that you are defining in
your class declaration here.
And as I said, you can use it everywhere in a field, in a constructor, in a method, and also as
a return type from a method.
Cool.
So that's it.
Let's move on to the next lesson.