Can someone explain clearly this example https://www.sololearn.com/learn/CSharp/2585 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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);

12th Aug 2019, 10:01 AM
DataStream Cowboy
DataStream Cowboy - avatar
5 Answers
+ 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());
12th Aug 2019, 10:09 AM
Dlite
Dlite - avatar
12th Aug 2019, 10:06 AM
Trigger
Trigger - avatar
+ 1
Doesnt seem to be anything wrong with it
12th Aug 2019, 10:08 AM
Trigger
Trigger - avatar
0
Yes of course.... But am just confused cause i expect the output to be "you are (age that a user has input)
12th Aug 2019, 10:11 AM
DataStream Cowboy
DataStream Cowboy - avatar
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
12th Aug 2019, 10:19 AM
DataStream Cowboy
DataStream Cowboy - avatar