Venciity

[Jumpstart C#] Console Demos - Try Parse

Jan 22nd, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.41 KB | None | 0 0
  1. using System;
  2.  
  3. class TryParseExample
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Write("Insert a number: ");
  8.         string str = Console.ReadLine();
  9.         int number;
  10.         if (int.TryParse(str, out number))
  11.         {
  12.             Console.WriteLine("Valid number: {0}", number);
  13.         }
  14.         else
  15.         {
  16.             Console.WriteLine("Invalid number: {0}", str);
  17.         }
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment