checked、is、as、sizeof、typeof運算符、空合併運算符

 //--------------------------------checked防止溢出
            byte b = 255;
            checked
            {
                b++;
            }
            Console.WriteLine(b.ToString());//byte類型只包含0~255,加上checked因此會拋出異常ide

            //is運算符,檢查對象是否與特定的類型兼容
            int i = 0;
            Console.WriteLine(i is object);//True對象

            //--------------------------------as運算符,顯示轉換特定的類型
            object o = "123";
            string s = o as string;
            Console.WriteLine(s);string

            //--------------------------------sizeof運算符,能夠肯定棧中值類型的長度
            Console.WriteLine(sizeof(int));//輸出4it


            //--------------------------------typeof運算符,返回一個特定類型的System.Type對象
            Console.WriteLine(typeof(string));//輸出System.String
           
            Console.ReadKey();class

             

 

            //--------------------------------可空運算符與空合併運算符object

            int? i = null;//int?可空運算符
            int? a = i ?? 10;//空合併運算符 (若是??前面是空則等於第二個值)
            Console.WriteLine(a);//輸出10
            Console.ReadKey();異常

相關文章
相關標籤/搜索