+ 2
Can someone explain clearly this example https://www.sololearn.com/learn/CSharp/2585
Why the code below as an example used in sololearn brings unhandled exception after i run the code. int age = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("You are {0} years old", age);
5 ответов
+ 4
That code above, takes whatever you type in the console and converts it into an integer.
By default, what ever you type in the console is treated as a String, so to read an integer, you have to "convert" that String to an integer by using
Convert.ToInt32(Console.readLine());
+ 1
Here is the code that works
https://code.sololearn.com/crOYf53EFa2J/?ref=app
+ 1
Doesnt seem to be anything wrong with it
0
Yes of course.... But am just confused cause i expect the output to be "you are (age that a user has input)
0
D'Lite i got you ...but is this code alternative fine
string age = Console.ReadLine();
Convert.ToInt32(age);
Console.WriteLine("you are (0) years old", age);
*i used (0) because the place holder is not in my keyboard and the code above i didnt test yet