Get a number from the console

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
C# Code Snippets

C# Source Code Bank

See also …
edit

This C# code snippet obtains a string from the console and converts it to a number.

try
{
   int number = Convert.ToInt32 (Console.ReadLine());
   Console.WriteLine ("Number: " + number);
}
catch (FormatException e)
{
   Console.WriteLine (e.Message);
}
 
// or ...
 
try
{
   int number = Int32.Parse (Console.ReadLine());
   Console.WriteLine ("Number: " + number);
}
catch (ArgumentNullException e)
{
   Console.WriteLine (e.Message);
}

Personal tools