new-style (v2.0) try-parse pattern : Convert from string : Data Type C# Examples


C# Examples » Data Type » Convert from string »

 

new-style (v2.0) try-parse pattern









    
using  System;
using  System.Collections.Generic;
using  System.Globalization;
using  System.IO;
using  System.Text;


public  class  MainClass
{
        public  static  void  Main()
        {

                string  s  =  "bad  format";
                int  i;
                if  (int.TryParse(s,  out  i))
                {
                        //  Use  'i'
                }
                else
                {
                        //  Handle  the  error;  e.g.  tell  the  user  input  was  invalid.
                        Console.WriteLine("Input  invalid");
                }
        }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo Data Type
» Convert from string