I\'m a beginner who is learning .NET.
I tried parsing my integer in console readline but it shows a format exception.
My code:
using System;
If it's throwing a format exception then that means the input isn't able to be parsed as an int
. You can check for this more effectively with something like int.TryParse()
. For example:
int age = 0;
string ageInput = Console.ReadLine();
if (!int.TryParse(ageInput, out age))
{
// Parsing failed, handle the error however you like
}
// If parsing failed, age will still be 0 here.
// If it succeeded, age will be the expected int value.