Welcome to the practical examples of using classes.
So let's go ahead and create a new console app which I'm going to call classes app.
And then I'm going to select this checkbox here.
It's really important because otherwise it will be a little tricky.
So do not use top level statements is activated I'm going to click on Create and Create a new project.
So here you can see we already have this class keyword in our application.
And that's because C sharp is an object oriented programming language which has this class program,
which is the starting point of our application with its main method.
That's where the code is executed.
So this is the code that is the starting point of our app.
And it will be executed once we click on run.
Okay.
Now how do we create a new class.
Because that's what it's all about.
We've seen how to use this part of the application.
Well in order to create a new class, what you can do is you can open your solution explorer by clicking
on View Solution Explorer.
And here on the right hand side you can right click on your project.
And then you can go over to add.
And here select class at the bottom.
And here you can now give this class a name.
So I'm going to call this class car.
So the file will be car dot cs.
So let me click on add.
And you see this class has a bunch of boilerplate code.
So code that just is there and is unchangeable.
So for example it has those using directives.
Well we can delete them because we're not using them at this point.
But it's fine to just leave them as they are.
So just collapse them together.
Then we have this namespace class app, which is the same namespace we had for our other class program,
which was automatically generated for us.
And then we have our own class called car.
So you see this keyword class and you see this keyword or this name car.
Now if you see this purple color in Visual Studio, then you know that you are looking at a class.
So you can see that here as well.
Program has this purple color and console has this purple color.
So you can see console is also a class.
So this class is internal which just means that it can only be accessed from within the same assembly
and an assembly to really simplify it within the same program.
Okay.
So we can only use this class within the same program.
Other programs cannot access this class.
That's what this internal does for us.
So if you go into your class you can see that it directly prepares a little bit of code for you or recommends
to use this public car and then curly brackets.
So this here is called a constructor okay.
A constructor is very similar to a method, but it's different in that it has the same name, exact
same name as the class itself, and it does not have a return type.
So this constructor is called slash, executed whenever a new object of car is created.
So now what does all of that mean?
Let's look at this with an actual practical example where you can then see this in action.
So here I'm going to just use a console Writeline statement where I'm going to say an object of car
has been created.
Okay.
So that's what this constructor should execute once an object of car is created.
So basically this code here, whatever is in the curly brackets here will be executed whenever we create
a new item or object that is of type car.
How do we do that.
Well let's save the car class.
Let's go over to our program class.
And here let's go ahead and create an object of our own class of our car class so we can go ahead and
say car.
And then we can give it a name and then we can say new car.
So I'm going to call this car Audi.
All right.
So it will just be an Audi which will be a new car.
So with this new keyword we have seen it before.
But basically what it does is it allocates space inside of the memory for our car object.
Technically, what we are saying is create an object that follows the blueprint of my class car.
Now what is a blueprint?
A blueprint is basically the instructions and the rules that we define how this class is supposed to
behave.
And that's what we define in here.
So in our class here we can set it up as our blueprint.
And each time that we create an object of it, it will follow the rules that we have defined in this
blueprint.
Okay.
So in this case, the only rule that we have defined so far is that whenever we create an object of
car that we want.
This statement to be executed, where we just print something onto the console saying an object of car
has been created.
So this constructor will now be called once this line is executed.
So let me add console dot read key here so that our application doesn't stop.
And let me add a debug point here.
And let's run this just so you see what's going on okay.
So we see nothing is written onto the console.
We get to this line of code where we say car Audi new car.
So a new object of our car class is created.
So we are in this line.
Then let's for once step into which means we're going to step into the line of code which executes something
somewhere else, where you see jumps into our car class.
So you see internal class car and it executes this line here which says public car, which is our constructor,
which then in the next line executes this Console.WriteLine statement.
And then once it's done with this constructor code, it goes back here.
Now we have an Audi in the next line.
You see if you hover over Audi it says Audi contains classes app dot car.
So it's an object of car.
And then it executes the next line which will be hello world.
So if we now look at this we see an object of car has been created.
And then it says hello world.
Okay.
So now here, that's where creating an object of the class car or creating an instance of the class
car.
Okay.
So that's a different way of saying it.
But this is our first little object and we called it Audi.
And with this new keyword we assigned a value to it because before that was just an empty shell.
And now we assigned a new car to it.
We allocated some space in the memory and we now made this Audi available.
Okay, just as a last point, let's go ahead and create another object of car okay.
So I'm going to create another car object.
This one will be a BMW which will also be a new car okay.
So if we run this code now we will see that it just says an object of car or what did we say here?
An object of car has been created and it says that twice because we are creating two objects and both
objects are currently not modified at all.
But that is something we can change.
We can modify the information that a car has about itself.
So we can modify that.
An Audi knows that it's an Audi.
And then then BMW knows that it's a BMW.
And that's what we're going to look at in the next video.
So see you there.