This because you read a char.
Use appropriate methods like ReadInt32()
that takes care of a correct conversion from the read symbol to the type you wish.
The reason why you get 49
is because it’s a char code of the ‘1’ symbol, and not it’s integer representation.
char code
0 : 48
1 : 49
2: 50
...
9: 57
for example: ReadInt32()
can look like this:
public static int ReadInt32(string value){
int val = -1;
if(!int.TryParse(value, out val))
return -1;
return val;
}
and use this like:
int val = ReadInt32(Console.ReadLine());
It Would be really nice to have a possibility to create an extension method
, but unfortunately it’s not possible to create extension method on static type and Console is a static
type.