C# TryXXXX模式

   public static int? TrayParse(string text) {

             int ret;

             if (int.TryParse(text,out ret)) {

                 return ret;

             }

             else {

                 return null;

             }

         }



static void Main(string []argd){



 int? parsed = TrayParse("Not Valid");

            if (parsed.HasValue) {

                Console.WriteLine("Parsed to {0}",parsed.Value);

            }

            else {

                Console.WriteLine("Couldn't parse");

            }

}

  

你可能感兴趣的:(try)