+ 3

What is the problem here? Convert.ToString

What is the problem in this C# code ? Please send the correct code. static void Main (string [] args) { int num; num = Convert.ToString (Console.ReadLine ()); Console.WriteLine (num);

3rd Jan 2018, 9:58 PM
Tennouji Kotarou
Tennouji Kotarou - avatar
4 Answers
+ 4
The problem is that num is of type integer, but ReadLine() returns a string, converting it to string wont change anything. Since integer and string are incompatible, you need to cast to Int (Convert.ToInt32) in order to make it work.
3rd Jan 2018, 10:09 PM
Shadow
Shadow - avatar
+ 2
Help ?
3rd Jan 2018, 10:05 PM
Tennouji Kotarou
Tennouji Kotarou - avatar
+ 2
Thanks for Naitomea and sneeze
3rd Jan 2018, 10:14 PM
Tennouji Kotarou
Tennouji Kotarou - avatar
+ 1
What are you trying to do ? (I can guess, but do think for you self) There is a "int num", that is a integer Convert.ToString, what is the result of convert.TOstring -> a string A string cannot be assigned to a integer, That is the problem here.
3rd Jan 2018, 10:10 PM
sneeze
sneeze - avatar