0

How do I convert string to int?

This is my code: int number = Console.ReadLine(); if (number == 1) { Console.WriteLine("Good"); } But I have an error: cannot implicitly convert type 'string' to 'int' I do not know what to do. Please help.

11th Apr 2017, 7:07 PM
Luke Hegelund
Luke Hegelund - avatar
5 Answers
+ 5
Console inputs in c# are always string. Try using Convert.toInt32(Console.ReadLine() ) ; This will convert the input string to int. Likewise you need to convert your console inputs to required datatypes as it will always be string
11th Apr 2017, 7:13 PM
Shraddha
Shraddha - avatar
+ 4
int number = int.Parse(Console.ReadLine()); or int number; int.TryParse(Console.ReadLine(), out number);
11th Apr 2017, 7:29 PM
AtoMX
AtoMX - avatar
+ 3
Welcome @Luke Hegelund, happy to helpâ˜ș
11th Apr 2017, 7:20 PM
Shraddha
Shraddha - avatar
+ 1
Thanks, Shraddha, that really helped!
11th Apr 2017, 7:19 PM
Luke Hegelund
Luke Hegelund - avatar
0
fetched out every character out off the string and convert it into ascii code by using array and for loop
11th Apr 2017, 7:11 PM
Hunny
Hunny - avatar