+ 1

Cannot implicitly change from string to int

int x = Console.ReadLine(); Console.WriteLine("You are {0} years old today!", x); Whenever I put this code in, it tells me that I cannot implicitly change the variable from string to int, how may I work around this? I also wanted to add x++ so that it can show your age plus 1, however this isn’t applicable for string var, so that’s why I changed it. What should I do?

25th Jan 2022, 8:09 PM
Apokvund
Apokvund - avatar
1 Resposta
+ 3
Use the static Convert class: Convert.ToInt32(Console.ReadLine());
25th Jan 2022, 8:10 PM
Ani Jona 🕊
Ani Jona 🕊 - avatar