C-SHARP - 2.6 conversion
Hello everyone and welcome to this new video. We will see together how to convert string to an integer. So let's go, we'll see how to do it. First of all, I'll explain some small ones Things to know. Indeed, we are talking about the conversion of a Variable containing a string in a type int byte float etc. But if we have a number that is stored in the String variable. That is, if our variable strictly stores a Integer number and it is of type string then we can To convert. On the other hand, If our variable contains a number but also a letter, A word, then it will be impossible to Convert because you can't convert letters into a number. I'll show you an example, Here we are going to create a string variable Then we will give it as the name "test" For instance Then = And here we are in quotation marks the number 30. And don't forget them; If you want to convert this value into an integer of type Int for example, it will actually work Now If I add the word Hello next to the 30 Well, it won't work anymore because as I told you we don't Cannot convert a string into an integer. So here we remember what you must strictly have In quotation marks an integer To be able to convert it Now we'll see how Convert this number 30, currently to string type In an int type. To do this You must first create a variable of the type int because the Value of our variable will change, We must create a variable of the same type to which we want Convert our value Who here we want to convert the value into An int, or we create a variable of type int. Then we give it a name for example conversion After = And now mtn it's here that we're going to do our Conversion. We're going to use a function To transform a chain into an entire int And this function It starts with the type you want to convert to, so int Then we put a point. Parse() And in parentheses the variable you want to convert Who here, is test We will now try to display this value And we can see that it works. If now I want to play with this value by Example to add 1, well I can do it Because it is now considered a number I will test, after Console.WriteLine(); I'm going to write conversion = conversion + 1 Basically here I increment by 1, the value to store in Conversion, I add up And normally if everything goes well, The program must show us In the exit window 31. And as you can see, we have 31 that is displayed. In the previous video, we saw that when we did a + 1 With what the user to enter, The program directly transformed our 1 into a string Because the function that allows you to interact with The user recovers everything in string, Whether he types a number or not. And the computer runs the program. He's not going to say to himself" ah well he hit a Number I will directly convert it into a number", No. He doesn't know what The user will do with it so he has directly converted the + 1 in string and concatenate it with what the user has To enter. Small indication, This is specific to the latest version of C#, Some languages will directly show you that it It is not possible to make a +1 to a string Like the python for example. So now you know how Convert a string into an integer. I hope you liked this video and that everything is clear For you. I'll tell you next time In a new video.