1
Answer

How to get actual integer input from Console?

Photo of amit_gupta14

amit_gupta14

20y
1.9k
1
How to get actual integer input from Console? There are 2 scenarios. 1. If we use Console.Readline(), it will return string. We can use Int32.parse(string) to convert the input to integer. Is Console.ReadLine() is good if input is only one character say 'Y' 2. If we use Console.Read(), it will accept character and return integer. It is good for those input where we are sure that input is like 'Y' or 'N'. Suppose input is 'A'. Then Console.Read() will return 65. Again we have to convert it to char to get 'A'.IS not this extra step? My question is which is better general/common appraoch for any type of input

Answers (1)

0
Photo of bilnaad
NA 686 0 20y
No you need to convert to a char. This can be done with the Convert class. But also with casting the integer to a char. int c = Console.Read(); char result = (char)c;