C-SHARP - 2.3 Types and constant

Hello everyone and welcome to this new video. In this video we will see the different types that exist in C# first, then we will finish on the notion of constant. There are different types of data in C#, first we will see the primitive types. What are primitive types basically? What are they? These are data types that are incorporated into the C# language at the lowest level. Here is the list of the most commonly used primitive types in basic C# the list is a little longer, but I decided not to include the rest of the data types, because they are almost never used. See never. So it is better to favor those that are most used so as not to get too lost and if you are interested to learn more about other primitive types you can consult Microsoft's online documentation. As you can see, this table is composed of 4 categories. Integers, Real Numbers, Character, and finally Boolean. The first column designates the data types in C#, each word corresponds to a type in the structure of the . NET And when you compile your application, the compiler internally translates your code and will look for the equivalent of your C# type contained in your code in . NET For example, the int of your program will be an Int32 in . NET etc... Then the 3rd column will indicate how many bytes your type occupies in memory I have arranged this table so that it is from the smallest to the largest. In the category of integers, the one that takes the fewest bytes is the byte and the largest is along. After that, you don't necessarily need to remember that. The important thing you need to remember is above all, the last column. An byte for example can contain a value between 0 and 255 A short between -32,000 and 32,000 An int in -2 billion and 2 billion and for the long a very large value we will most often use the int type. For the category of real numbers, we have 3 types of data: Float, Double and Decimal The float is the one that is commonly used however if you need greater number or greater accuracy then you can use the others. Then we have the char type that corresponds to a character the char in C# corresponds to all Unicode characters. And finally, we have a bool that represents a Boolean that may be True or False. All types of this table are very simple However, there is a small peculiarity in C# for real numbers. I explain: As you can see in the category of real numbers, we have: The float, the Double and the Decimal. Here the double is highlighted because it is the one used by default by the C# compiler when you use real numbers. So if you want to declare a float then don't forget to tell the compiler that it is a float To do this, in addition to having given the variable as a type, you must put a suffix at the end of your number You must add an F just after the last digit of your number. As you can see below. If I had not put the "f" after, I would have had an error that would have been displayed to me because the compiler does not know if it should treat it as a float or a double since the default compiler defines a real number by a double and it is not possible to put a duplicate in a float type. So it's very important not to forget it. And it's the same for the decimal except that there the suffix will not be an "f" but a "m". So here we are, we are done with primitive types, but there are types in C# that are not primitive among them: There are strings as we saw in a previous video, arrays Enum, classes that correspond to strings, arrays, structures, classes. We will see all these notions little by little as we progress. Now let's move on to the second part of our video which will deal with the notion of constant. Let's go! We saw in the previous video the notion of variable, we can reuse this variable in our program, display the value it contains, modify it etc. For the constant it is the same except for one thing The constant cannot be modified Indeed, the value that is contained in the constant can be reused But can never be modified. You're going to tell me, but what exactly is it for? I'll take an example. Let's imagine that we create a fighting game. Each player has PVs. And you agree with me that the player has a maximum number of HP and minimum Basically his max is 100 and his minimum is 0 These are values that will not change during our program. The maximum HP will always be 100 and the minimum HP will be 0. The only thing that will change during the fight is the player's PVs not the max value or the minimum value, you understand? So that's what our constant will be for, to block the end of setting a value and that it doesn't change if we try to modify it in our program So we'll see now in C# How does it work? Let's go! Let's go to our FDI. Here we are now on our IDE, we can delete all this. and to create a constant, it's very simple you type const Then the type of data we will assign it here it will be a number so we can put an int Then the name of the constant, here it's the value min so we can call it minVie Then = and the value we The max value which is 100 and don't forget them too; And finally we can create a life variable that will correspond to the player's life in the game For this you type int life = 50 for example and don't forget it; We will perform some small tests to show you that we can't change the value of a constant To do this You go to the line We will assign to vieMax, the value contained in vieMax - 1 or 100 - 1 in fact. However, you can see that here Basically to change the value that vieMax contains, it must be a variable And we can't change the value of a constant So that's what the constant will serve us, that we can't modify values that must be the same during our program Now if I want to change the player's life I can quite do it because it's a variable. And this value can undergo changes if the player suffers damage Here I will first display life before the change. I make a Console. Write Line() And in parentheses I put the life variable and I don't forget them; Then below I make life = life - 10 for example; And I see it display again I go below, I type cw and I make a tab on my keyboard It's a small shortcut of the Console.WriteLine() And you put the life variable in parentheses I And after our life is now worth 40 So remember that, a constant is like a variable except that the constant has a constraint, its value does not change during the program. The constant will be used when we have values in our program that does not change as the above case of a video game or otherwise if we create a payment system for a site and we know that VAT will never change, it will always be 20%. So here we will also use a constant The cases are very numerous after I'm not going to all the cities In any case I hope it's clear to you and that you liked this video then in a new life.