[C#][Tips]比較Int32.TryParse、Int32.Parse And Convert.ToInt32

  • 23492
  • 0
  • C#
  • 2010-01-20

[C#][Tips]比較Int32.TryParse、Int32.Parse And Convert.ToInt32

今天被朋友問了Int32.TryParse Int32.Parse 和Convert.ToInt32到底差在那裏

一時間無法很明確說出差異,所以再參考MSDN後就記錄下來。

MSDN解釋如下

Int32.TryParse:將數字的字串表示轉換成它的對等 32 位元帶正負號的整數。傳回指示轉換是否成功的值。

Int32.Parse:將數字的字串表示轉換成它的對等 32 位元帶正負號的整數。

Convert.ToInt32:將數字的指定 String 表示轉換為相等的 32 位元帶正負號的整數。

雖然三種方法都是將內容轉換為相等的32位元帶正負號整數

但結果會因為資料不同而大不同,這裡簡單測試記錄。 

            String data = "123456";
            Int32 myint = 0;          
            Int32.TryParse(data,out myint);
            Console.Write(myint + "\r\n");          

            myint = Int32.Parse(data);
            Console.Write(myint + "\r\n");

            myint = Convert.ToInt32(data);
            Console.Write(myint + "\r\n");

            Console.ReadLine();

得到相同的結果(預料中)

image

改一下資料(data=””) 

String data = "";
            Int32 myint = 0;          
            Int32.TryParse(data,out myint);
            Console.Write(myint + "\r\n");          

            myint = Int32.Parse(data);
            Console.Write(myint + "\r\n");

            myint = Convert.ToInt32(data);
            Console.Write(myint + "\r\n");

            Console.ReadLine();

結果:Int32.TryParse回傳0(false),但Int32.Parse和Convert.ToInt32都會發生格式錯誤的Exception

image image image

 

再改一下資料(data=null)

String data = null;
            Int32 myint = 0;          
            Int32.TryParse(data,out myint);
            Console.Write(myint + "\r\n");

            myint = Convert.ToInt32(data);
            Console.Write(myint + "\r\n");

            myint = Int32.Parse(data);
            Console.Write(myint + "\r\n");          
            
            Console.ReadLine();

結果:Int32.TryParse和Convert.ToInt32都回傳0,Int32.Parse依然發生Exception

image image

由於TryParse型別為bool所以不會拋出任何Exception,只會回傳true(1)或false(0)表式解析是否成功。

Convert.ToInt32型別雖然是int但是遇到null會返回0。

Int32.Parse都會拋出Exception

 

接下來測試這三種再解析效能如何(測試5次取平均)

String data = "123456";
            Int32 myint = 0;
            Stopwatch sw = new Stopwatch();
            sw.Reset();
            sw.Start();
            for (Int32 i = 0; i < 500000; i++)
            {
                Int32.TryParse(data, out myint);
            }
            sw.Stop();
            Console.Write(myint + "_Int32.TryParse時間:" + sw.ElapsedMilliseconds.ToString() + "\r\n");

            sw.Reset();
            sw.Start();
            for (Int32 i = 0; i < 500000; i++)
            {
                myint = Convert.ToInt32(data);
            }
            sw.Stop();
            Console.Write(myint + "_Convert.ToInt32時間:"+sw.ElapsedMilliseconds.ToString()+"\r\n");           

            sw.Reset();
            sw.Start();
            for (Int32 i = 0; i < 500000; i++)
            {
                myint = Int32.Parse(data);
            }
            sw.Stop();
            Console.Write(myint + "_Int32.TryParse時間:" + sw.ElapsedMilliseconds.ToString() + "\r\n"); 
            Console.ReadLine();

 第一次

image

第二次

image

第三次

image

第四次

image

第五次

image

總表

Int32.TryParse平均Convert.ToInt32平均Int32.Parse平均
192.6195.4195.4

 看來三種方法其實都差不多。但Int32.TryParse似乎比較好(保哥說明:Exception cost較其他兩種方法低)。

而Convert.ToInt32和Int32.Parse相同其實也是可理解的,因為Convert.ToInt32最終會由Int32.Parse來處理

MSDN備註傳回值是在 value 上叫用 Int32..::.Parse 方法的結果