C#.NET中的類型轉換(關鍵字checked和unchecked)

來源:互聯網
上載者:User
/*
 * Created by SharpDevelop.
 * User: noo
 * Date: 2009-8-17
 * Time: 11:10
 * 
 * 類型轉換(關鍵字checked和unchecked)
 */
using System ;
using System .Windows .Forms ;
class Test
{
    static void Main()
    {
//        int a=5;
//        long b;
//        b=a;//隱式轉換
//        Console.WriteLine (b);
        
//        int aa;
//        long bb=5;
//        aa=(int)bb;//顯式轉換。由於as運算子必須與參考型別一起使用,而“int”是實值型別,故這裡不可寫作aa=bb as int;
//        Console.WriteLine (aa);
        
//        int aaa;
//        long bbb=12345678901;
//        try
//        {
//            aaa=checked((int)bbb);//檢測是否有異常
//        }
//        catch (System.OverflowException)
//        {
//            MessageBox.Show ("有溢出異常");
//            return;
//        }
//        Console.WriteLine (aaa);
        
        int aaaa;
        long bbbb=12345678901;
        unchecked//checked和unchecked語句的兩種寫法
        {
            aaaa=(int)bbbb;//unchecked 關鍵字用於取消整型算術運算和轉換的溢出檢查。
        }
        Console.WriteLine (aaaa);//輸出隨機資料
    }
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.