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); } }
source share