隱式轉換和顯式轉換及強制轉換的區別【轉】

來源:互聯網
上載者:User

標籤:nbsp   --   情況   stat   引用   運行   最佳化   format   包含   

隱式轉換和顯式轉換及強制轉換的區別

string strType = "123"; object objType= (object)strType;//可以不要,隱式轉換, 要的為顯式轉換 string strType2 = (string)objType; //必須要,顯式轉換

int intType = (int)strType; //錯誤,不能通過編譯 int intType = (int)objType; //錯誤,能通過編譯

int intType = Convert.ToInt32(objType); //正確, 強制轉換 int intType = int.Parse(strType); //正確, 強制轉換 int intType = int.Parse(objType); //錯誤,不能通過編譯   --------------------------------------------------------------

  在 C# 中,(int),Int32.Parse() 和 Convert.toInt32() 三種方法有何區別?

    int 關鍵字表示一種整型,是32位的,它的 .NET Framework 類型為 System.Int32。

    (int)表示使用顯式強制轉換,是一種類型轉換。當我們從 int 類型到 long、float、double 或decimal 類型,可以使用隱式轉換,但是當我們從 long 類型到 int  類型轉換就需要使用顯式強制轉換,否則會產生編譯錯誤。

    Int32.Parse()表示將數位字串轉換為32 位有符號整數,屬於內容轉換[1]。     我們一種常見的方法:public static int Parse(string)。     如果 string 為空白,則拋出 ArgumentNullException 異常;     如果 string 格式不正確,則拋出 FormatException 異常;     如果 string 的值小於 MinValue 或大於 MaxValue 的數字,則拋出 OverflowException 異常。

    Convert.ToInt32() 則可以將多種類型(包括 object  參考型別)的值轉換為 int  類型,因為它有許多重載版本[2]:     public static int ToInt32(object);     public static int ToInt32(bool);     public static int ToInt32(byte);     public static int ToInt32(char);     public static int ToInt32(decimal);     public static int ToInt32(double);     public static int ToInt32(short);     public static int ToInt32(long);     public static int ToInt32(sbyte);     public static int ToInt32(string);     ......

    (int)和Int32.Parse(),Convert.ToInt32()三者的應用舉幾個例子:   

    例子一:

    long longType = 100;     int intType  = longType;       // 錯誤,需要使用顯式強制轉換     int intType = (int)longType; //正確,使用了顯式強制轉換

    例子二:

    string stringType = "12345";     int intType = (int)stringType;                //錯誤,string 類型不能直接轉換為 int  類型     int intType = Int32.Parse(stringType);   //正確

    例子三:

    long longType = 100;     string stringType = "12345";     object objectType = "54321";     int intType = Convert.ToInt32(longType);       //正確     int intType = Convert.ToInt32(stringType);     //正確     int intType = Convert.ToInt32(objectType);    //正確

    例子四[1]:

    double doubleType = Int32.MaxValue + 1.011;     int intType = (int)doubleType;                                //雖然運行正確,但是得出錯誤結果     int intType = Convert.ToInt32(doubleType)            //拋出 OverflowException 異常

   (int)和Int32.Parse(),Convert.ToInt32()三者的區別:

    第一個在對long 類型或是浮點型到int 類型的顯式強制轉換中使用,但是如果被轉換的數值大於 Int32.MaxValue 或小於 Int32.MinValue,那麼則會得到一個錯誤的結果。

    第二個在符合數字格式的 string 到 int  類型轉換過程中使用,並可以對錯誤的 string 數字格式的拋出相應的異常。

    第三個則可以將多種類型的值轉換為 int 類型,也可以對錯誤的數值拋出相應的異常。

    無論進行什麼類型的數值轉換,數值的精度問題都是我們必須考慮的[1]。

-------------------------------------------------------- 使用Convert.ToInt32()把一個char型轉換成int時,是把這個char的ascci碼給過去而不是數字 如: char c = ‘1‘; int i; i = Convert.ToInt32(c); //char需注意的事項 //這時i的值為49,是1的ascii碼

想得到1,可以使用string類型, 如: string str= "1"; int i; i = int.Parse(str); i = Convert.ToInt32(str); //這時i的值為1,而不是1的ascii碼

----------------------------------------------------

隱式轉換各顯式轉換要求是同類型的,就是說兩種資料類型必須相容,隱式轉換是向上轉型(相當是子類轉父類),而強制類型轉換則是向下轉型(相當是父類轉子類),就好像Double型的可以包含int型一樣。 而強制轉換可以是不是同一種類型,(如同class1與class2同層級的類一樣),兩都進行內容上的解析。Convert.ToInt32與int.Parse都是強制轉換,int.Parse是轉換String為int(這種情況很多,可能進行了些最佳化,也可能只是為了方便,處理邏輯一樣), 而Convert.ToInt32是轉換繼承自Object的對象為int的(18種重載). 比如一個object對象,你想把它轉換為int,用int.Parse就不可以,要用Convert.ToInt32

隱式轉換和顯式轉換及強制轉換的區別【轉】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.