"FormatException" is not thrown when the ToInt32 () method has an invalid argument type

one)

class Program { static void Main(string[] args) { int a; a = Convert.ToInt32( "a" ); Console.Write(a); } } 

I get a FormatException with the message: Input string was not in a correct format . and this is understandable.

2)

 class Program { static void Main(string[] args) { int a; a = Convert.ToInt32( Console.Read() ); Console.Write(a); } } 

In the second case, I can enter any characters, for example abc and appear in the console.

Question: Why not throw a FormatException in the second case, and why does it work successfully with int characters?

UPDATE

using the Console.ReadLine() method, which returns a string , also did not FormatException and successfully printed any character in the console.

 class Program { static void Main(string[] args) { int a; a = Convert.ToInt32(Console.ReadLine()); Console.Write(a); } } 
+4
source share
5 answers

The return type of Console.Read() is an int .

Then you call Convert.ToInt32(int) :

Returns the specified 32-bit signed integer; The actual conversion is not performed.

+6
source

Since the output of Console.Read() is int . This means that it gets an int representation of what you typed, so if you type a character, it actually gets an internal representation of that character, and that's all right.

To find out what happens in detail:

 int a; a = Convert.ToInt32(Console.Read()); //input for example: abc Console.WriteLine(a); //97 Console.WriteLine((char)a); //a 
+5
source

Return Value Type: System.Int32 The next character from the input stream, or negative (-1) if there are currently no more characters to read.

 public static int Read() 

Link

FormatException . The value does not consist of an optional character followed by a sequence of digits (0 to 9).

+4
source

I strongly suspect that you are mixing Console.ReadLine and Console.Read methods.

From Console.Read doc ;

Reads the next character from standard input.

and

Return Value Type: System.Int32 The next character from the input stream, or negative (-1) if there are currently no more characters to read.

This means that when you put <T23> using this method returns 97 ( because 97 is the ASCII value of the first character>), which is a valid integer.

+3
source

https://msdn.microsoft.com/en-us/library/sf1aw27b(v=vs.110).aspx

ToInt32 has an overloaded version that can take a string, but the string must be a real number representation; "a" is not a number, but if you have "101", it will parse correctly -

 int a; a = Convert.ToInt32("101"); //will parse to int Console.Write(a); a = Convert.ToInt32("a"); //will not parse to int Console.Write(a); 

The reason your second example works while the first does not work is because Console.Read returns an integer value based on the next character passed into it, so everything is fine when you call ToInt32 with it.

UPDATE -

Just tested it using ReadLine, and it still gave me an error.

enter image description here

+3
source

All Articles