C-SHARP - 2.2 The variables

Hello everyone and welcome to this new video. So we'll see together the notion of Variable and how to use them? A wholesale variable is a kind of box to which we will assign a name and which will store data in memory. I will first start with a simple example. And you will see that with these variables we will be able to manipulate data in our program. What I would like to do here is that instead of having things in quotation marks is to put in parentheses a variable that will store a sentence for example "Hello, today is Tuesday and I have to go to school". To do this, just above we will create a variable. I give it a name for example I will call it intro sentence. However, you see that here there is an error, it will not work because indeed there are rules in C# language to respect. You can't put spaces between words or special characters. You just have to paste the different words by putting capital letters or you can put underscores. In general, developers work in this way. Now, our variable will be given a value. To do this you have to put a =, and we put in quotation marks the sentence we wanted to put "Hello, today is Tuesday and I have to go to school". And don't forget it; in the end. We always end a line with one; in C# except in some cases we will see later. You see that here we have an equal, in reality it will allow us to assign a value to a variable and store it somewhere in memory. It's not like in math, or equalization allows us to do an equality test like 2 + 3 = 5 no. Here it's as if we had a jar and put a candy in it. This jar will not be equal to a candy, it will always be equal to the jar but this time it will contain a candy. So for variables it's the same. The equal sign will allow us to give a value to a variable, which here the value of the variable "entenceIntro" is the string "Hello, today is Tuesday and I have to go to school" that we can also reuse or modify later in our program. Except that here, you see we have an error, I am missing something, indeed a variable will contain data of such types. You see that here we have a string, and this is very important in C#, to specify the type of data to our variable it will contain. And to do this, you must write string in front of the name of our variable. String means string in English. A variable in C#, consists of the type of data it will contain, then its name, an equal and the value you want to assign it. So for summary, here we created a variable by specifying its type: string type. Then it is given a name "entenceIntro". And it is simply assigned a value. "Hello, today is Tuesday and I have to go to school." Small parenthesis; remember that the string corresponds to a string. because when you store a number, the type of the variable will change. It will no longer be string but an Int type. For a decimal number, it will be a float type etc. I'll tell you about it later. Now you see that this is underlined. However, this is only a warning, basically we have a variable but we do not use it in our program. So we will now use it. To do this, we will delete all this and we will simply display what is in the variable for the moment. You type Console. WriteLine In parentheses and here you enter the name of the variable we just created "phraseIntro". and don't forget them; We launch the program. And you see, we posted "Hello, today is Tuesday and I have to go to school". Here what happened concretely, we gave our Console.WriteLine() function the sentenceIntro variable and displayed the value contained in this variable. Now we will play a little and modify our program We will remove our variable "sentenceIntro", and we will create a variable "name" type string and an age variable" type string that will contain a person's name and age. And we will try to display in the exit window "Hello, my name is Jibril and I am 18 years old. » Jibril will be contained in the variable "name" and 18 in the variable "age". Here I could have directly put an int type for the age variable because it is a number. In C# depending on the version, he takes direct care of converting an int to a string because in some languages it is not possible to concatenate an int with a string. On the other hand, here I didn't do it because we don't yet have the other types they exist it will be the subject of the next video. So we will concatenate, basically paste several strings together. To do this, you type in parentheses "Hello, my name is then after the quotation marks you put a + The variable "name" +" and I have "you put a + after the quotation marks, the age variable. And finally you put + "years" and don't forget the semicolon at the end. Attention I specify that the most must always be outside the quotes, because otherwise we will not get a different result than what we want. I will now launch the program. And you can see that here, I wrote well Hello, my name is Jibril and I am 18 years old The program worked well so here what happened? We concatenate several strings with variables that themselves contain strings that we retrieved to have this result on the screen. Now you can tell me but what's the point of doing all this, you might as well write everything directly between quotes Our goal is to create reusable programs. Here we wrote a person's name directly but later we will see together how to get the computer to ask the user to enter his name, when we launch the program then he will store this value in a variable and we can use it as we did here In the next video, we will see the different types of variable in C#. So I'll tell you right away in a new video.