Dot-Net-1.1

.net 1.1 的 int.TryParse 的最佳替代品是什麼

  • May 31, 2012

使用 .net 1.1 執行等效於 int.TryParse (在 .net 2.0 及更高版本中)的最佳方法是什麼。

明顯地,

class Int32Util
{
   public static bool TryParse(string value, out int result)
   {
       result = 0;

       try
       {
           result = Int32.Parse(value);
           return true;
       }
       catch(FormatException)
       {            
           return false;
       }

       catch(OverflowException)
       {
           return false;
       }
   }
}
try
{
   var i = int.Parse(value);
}
catch(FormatException ex)
{
   Console.WriteLine("Invalid format.");
}

引用自:https://stackoverflow.com/questions/709336