c# 資料類型

來源:互聯網
上載者:User

標籤:

 

整數類型
           
類型 大小 範圍(包括邊界值) BCL名稱 是否有符號 尾碼
sbyte 8位 -128~127 System.SByte Yes  
byte 8位 0~255 System.Byte No  
short 16位 -32768~32767 System.Int16 Yes  
ushort 16位 0~65535 System.UInt16 No  
int 32位 -2147483648~2147483637 System.Int32 Yes  
uint 32位 0~4294967295 System.UInt32 No U或u
long 64位

-9223 3720 0368 755 808~

9223 3720 0368 755 807

System.Int64 Yes L或l
ulong 64位 0~18 466 744 073 709 551 615 System.UInt64 No UL或ul
 

 

 

浮點類型
類型 大小 範圍(包括邊界值) BCL名稱 有效數字 尾碼
float 32位   System.Single 7  F或f
double 64位   System.Double 15~16  
 

 

 

decimal類型
類型 大小 範圍(包括邊界值) BCL名稱 有效數字 尾碼
decimal 128位   System.Decimal 28~29  M或m
 

 

/*C#文法基礎 * 1.C#程式是從Main方法開始執行。要求Main方法的傳回值類型為void或int ,而且要麼不帶參數,要麼接受一個字串數組作為參數。當傳回值是int的是狀態嗎,標誌程式是否執行成功,返回非 * 0值通常意味著錯誤; * 2.類型:是具有相似特徵和行為的個體的分類; * 3.一次賦值返回一個值,所以C#允許在同一條語句中連續進行多個賦值操作; * 4.基元類型:8中整數類型,2種二進位浮點類型,1種金融計算的額十進位浮點類型,1種布爾類型,1種字元類型,對於這13中基元類型都是struct類型; * 那也就是說這些都是實值型別,也是是說都是派生自System.ValueType; * 5.對於8種整形來說存到局部變數(locals)裡面都是int(8~64),uint(8~64)類型,在32位以前都是一int4(32位)載入,long和ulong轉換成i8(64位) * 6.對於浮點類型來說:float,double分別是以r4和r8載入的,儲存到局部變數中分別是float32和float62; * 7.對於deciaml 來說載入的時候是以i4載入的,在調用System.Decimal的構造進行執行個體化,儲存到局部變數中是valueType [mscrolib]System.Decimal類型 * 8.格式說明符:“{0:x/X}”表示以16進位,"{0:R/r}"表示字串轉換為數值 * 9.bool類型的實際大小是一個位元組 * 10.char 類型是2個位元組 * 11.null只能賦給參考型別,可空實值型別,指標 * 12.?可空修飾符;可以將null賦給實值型別;在資料庫編程中尤其有用; * 13.大小不一致的multidimensional Array會造成編譯錯誤; * 14.交錯數組:就是由數組構成的數組。交錯數組不用逗號標識新的維,交錯數組定義由數組構成的數組。 * 15.[]數組訪問符 * 16.為了避免overflow最好用數組.Length-1來避免月結錯誤;不要使用寫入程式碼 * User: YuanWei * Date: 2015/1/20 * Time: 9:36 *  */using System;namespace LearningBasis{    class Program    {        public static void Main(string[] args)        {            // TODO: Implement Functionality Here//            int rt=System.Console.Read();//返回的是對應的ascii的十進位整數,            /*8種整形:sbyte,byte,short,ushort,int,uint,long,ulong*///            sbyte sbNum=1;//8bits BCL:System.SByte//            byte bNum=2;//BCL:System.Byte//            short shNum=3;//16bits BCL:System.Int16//            ushort ushNum=4;//BCL:System.UInt16//            int iNum=5;//32bits BCL:System.Int32//            uint uiNum=6U;//有尾碼 BCL:System.UInt32//            long lNum=7L;//有尾碼//64bits BCL:System.Int64//            ulong ulNum=8UL;//有尾碼BCL:System.UInt64//            /*3種浮點類型:float,double,decimal(特殊的浮點類型)*///            float fNum=23.6f;//BCL:System.Single//            double douNum=24.8;//BCL:System.Double//            decimal decNum=45.9m;//BCL:System.Decimal//            /*bool,char*///            bool isReal=false;//BCL:System.Boolean//            char word=‘a‘;//BCL:System.Char//            /*string*///            string strExample1="nihao";//            string strExample2="nihao";//            string strNull=null;//            if(strNull==null)//            {//                Console.WriteLine("strNull is Null");//            }//            Console.WriteLine("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}",sbNum,bNum,shNum,ushNum,iNum,uiNum,lNum,ulNum,fNum,douNum,decNum,isReal,word);//            Console.WriteLine(strExample1);//            Console.WriteLine(strExample2);////            //Display "Ox2A"//            Console.WriteLine("0x{0:X}",42);            //Fightable fight=(new Person("zhang")) as Fightable;//將Person類型轉換為父介面Fightable            //fight.Fight();//            Person p=new Person();//            p.person=new Person();            //Person p=new Person("xiaonizi"){person=new Person("haoren")};            //p.person.Fight();//            unchecked//強制不做溢出檢查,checked做溢出檢查//            {//                int maxNum=int.MaxValue;//2147483647//                maxNum+=1;//                Console.WriteLine(maxNum);//            }            /*先聲明後賦值*///            int [] arrayNum1;//             arrayNum1=new int[3];//onw dismentional//            arrayNum1={11,22,33};//錯誤//             int[,] arrayNum2;//            arrayNum2=new int[2,3];//two dismentional//            int [,,] arrayNum3;//             arrayNum3=new int[2,3,4]; //three dismentional//            int [,] cells=int[3,3];//            cells={{1,2,3},{1,2,0},{1,2,1}};//錯誤//            arrayNum2={{1,2,3},{1,2,3}};//錯誤            /*聲明時賦值*///            int [] arrayNum11={1,2,3};//            int [,] arrayNum22={{1,2},{1,2},{2,3}};            int [,,] arrayNum33={{{11,23},{22,45}},{{33,67},{44,58}}};//            int[] arrayNum44=new int[]{0,1,2};//            int[] arrayNum55=new int[3]{1,2,3};//其實不同寫多少個,但是一但寫了之後,在後面的賦值就必須有固定個數,不能多也不能少//            foreach(int num in arrayNum33)//            {//                Console.WriteLine(num);//            }//            string[] strArray=new string[3];//            Console.WriteLine(strArray[0]);//            int[] test=new int[3];//            Console.WriteLine(test[1]);            //Console.WriteLine(arrayNum33[1,1,1]);//訪問數組裡面的元素            //Console.WriteLine("{0},{1},{2}",arrayNum1.Length,arrayNum2.Length,arrayNum3.Length);            /*交錯數組:解析:int[]是資料類型,所以他之後的[]聲明了一個int[]類型的數組,注意,交錯數組要求為內部的每個數組都建立數組執行個體*/            int[][] cells={                new int[]{1,0,2,0},                new int[]{1,2,0},                new int[]{1,2},                new int[]{1}            };            cells[0][1]=3;//訪問交錯數組中的元素            Array.Reverse(cells);//            Array.Clear(cells,0,cells.Length-2);//            foreach(int num in cells)//會報錯:無法將類型“int[]”轉換為“int”也就是說明上面的一個數組的類型是int[],//            {//                Console.WriteLine(num);//            }//            foreach(int[] num in cells)//num遍曆到的是每一個int數組,換句話說,就是cells裡面的元素是數組//            {//                foreach(int subNum in num)//subNum表示的是每一個數組裡面的元素//                {//                    Console.Write(" "+subNum+" ");//                }//                Console.WriteLine();//            }            Console.ReadKey(true);        }    }    interface Fightable    {        void Fight();    }    class Person:Fightable    {        public Person(string name )        {            this.Name=name;        }        public Person person{get;set;}        public string Name{get;set;}        public void Fight()        {            System.Console.WriteLine("I can fight");        }    }}

 

c# 資料類型

聯繫我們

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