C-SHARP - 3.3 Class (demo)
We now find ourselves on Visual Studio to create a class. To create a class, it's very simple, you just go to the top and you have to fill in the access modifier here. Here you will determine if your class is accessible everywhere in your application. or if you want to limit its access wholesale one determines who can access this class. For my part here we will put it in public so that it is accessible in all my application. For now don't worry, we will see all the access modifiers in detail later in this course. For the moment remember that when you want to create a class, use the public keyword to make it accessible anywhere in our application. Once you have filled in the access modifier you must add the keyword class to it. And then we assign a name to our class here I call it Car. And opens the braces In our class, as I said in the previous video, can contain attributes or methods For example here, we will create attributes that will specify the mark, the year and the color of the car. And as for the classes, we must specify an access modifier that will make our attributes accessible through our entire application or not. So here I'm going to type public Then the type It's a mark so we're going to put a string and the name of my attribute, which I'm going to call "mark" Note that in C# when you have a declaration like an attribute declaration here. You must end this statement with a semicolon. We will also create the other attributes year and color we go to the line and we will write public. Then we will put an int type because it is a year We will give it the name year and we put a ; And below, the same for the color public String because it's a color we give it the name color and; Now that we are done with our attributes, we said that a class can contain attributes but also methods So here we are going to create a method that displays the details of our car So we are going to do what we are going to write public. Then void "void" We close the quotes a + and put the color attribute Also note that this method doesn't take any parameters, as indicated by an empty parenthesis, so it just displays what we put in the Console.WriteLine() Now that we have a class, let's see how to create an object. Creating an object is quite similar to creating a variable. We will first of all go to our main main method We start with the type, the type of my object will be the name of the class we have just created, which here is Car Then we will give a name for example " Here in our class we have not infected any value to our attributes in order to be able to create a multitude of cars the dotted notation will allow us to assign values to our attributes And to do that we will type the name of the object then . And there the name of the attribute. Who here is marked here the point will mean that my brand attribute belongs to the class Car then we will assign a value so we do = and for example the brand “Peugeot” and don't forget them; Then we do the same for the year and the color. I do car .year = 20192019 since c is an int; And for the color I do car.color = In quotes "blue"; Now that I'm done with my attributes. I'm going to get the function the method that will allow us to display the car So here I go to the line, I do a car.DisplayCar(); I run the program. and you see We have our car that was displayed. we have succeeded in displaying the characteristics of our object. On the other hand, the fact that my car class is in the Program.cs file is very restrictive Now let's imagine that we create an application. and that we put all our classes in a single file. Your program will not be optimized, will be slow and will saturate your memory So we are going to move our class into a file specific to it and to do this you click on the public just at the top. Where we have created the class and you see that we have a small blue icon here You click on it then you click on move to car.cs and it will automatically create a file for us with our class inside and go there remove from our main program And if we run the program. was another way to create a class in a faster way. In any case, that's it for the classes, I hope you liked this video and that everything was clear to you. See you next time in a new video.