码迷,mamicode.com
首页 > 编程语言 > 详细

C# 数据类型 数组,引用类型 值类型

时间:2015-01-22 23:17:25      阅读:513      评论:0      收藏:0      [点我收藏+]

标签:

  1 /*C#语法基础
  2  * 1.C#程序是从Main方法开始执行。要求Main方法的返回值类型为void或int ,而且要么不带参数,要么接受一个字符串数组作为参数。当返回值是int的是状态吗,标志程序是否执行成功,返回非
  3  * 0值通常意味着错误;
  4  * 2.类型:是具有相似特征和行为的个体的分类;
  5  * 3.一次赋值返回一个值,所以C#允许在同一条语句中连续进行多个赋值操作;
  6  * 4.基元类型:8中整数类型,2种二进制浮点类型,1种金融计算的额十进制浮点类型,1种布尔类型,1种字符类型,对于这13中基元类型都是struct类型;
  7  * 那也就是说这些都是值类型,也是是说都是派生自System.ValueType;
  8  * 5.对于8种整形来说存到局部变量(locals)里面都是int(8~64),uint(8~64)类型,在32位以前都是一int4(32位)加载,long和ulong转换成i8(64位)
  9  * 6.对于浮点类型来说:float,double分别是以r4和r8加载的,存储到局部变量中分别是float32和float62;
 10  * 7.对于deciaml 来说加载的时候是以i4加载的,在调用System.Decimal的构造进行实例化,存储到局部变量中是valueType [mscrolib]System.Decimal类型
 11  * 8.格式说明符:“{0:x/X}”表示以16进制,"{0:R/r}"表示字符串转换为数值
 12  * 9.bool类型的实际大小是一个字节
 13  * 10.char 类型是2个字节
 14  * 11.null只能赋给引用类型,可空值类型,指针
 15  * 12.?可空修饰符;可以将null赋给值类型;在数据库编程中尤其有用;
 16  * 13.大小不一致的multidimensional Array会造成编译错误;
 17  * 14.交错数组:就是由数组构成的数组。交错数组不用逗号标识新的维,交错数组定义由数组构成的数组。
 18  * 15.[]数组访问符
 19  * 16.为了避免overflow最好用数组.Length-1来避免月结错误;不要使用硬编码
 20  * User: YuanWei
 21  * Date: 2015/1/20
 22  * Time: 9:36
 23  * 
 24  */
 25 using System;
 26 
 27 namespace LearningBasis
 28 {
 29     class Program
 30     {
 31         public static void Main(string[] args)
 32         {
 33             // TODO: Implement Functionality Here
 34 //            int rt=System.Console.Read();//返回的是对应的ascii的十进制整数,
 35             /*8种整形:sbyte,byte,short,ushort,int,uint,long,ulong*/
 36 //            sbyte sbNum=1;//8bits BCL:System.SByte
 37 //            byte bNum=2;//BCL:System.Byte
 38 //            short shNum=3;//16bits BCL:System.Int16
 39 //            ushort ushNum=4;//BCL:System.UInt16
 40 //            int iNum=5;//32bits BCL:System.Int32
 41 //            uint uiNum=6U;//有后缀 BCL:System.UInt32
 42 //            long lNum=7L;//有后缀//64bits BCL:System.Int64
 43 //            ulong ulNum=8UL;//有后缀BCL:System.UInt64
 44 //            /*3种浮点类型:float,double,decimal(特殊的浮点类型)*/
 45 //            float fNum=23.6f;//BCL:System.Single
 46 //            double douNum=24.8;//BCL:System.Double
 47 //            decimal decNum=45.9m;//BCL:System.Decimal
 48 //            /*bool,char*/
 49 //            bool isReal=false;//BCL:System.Boolean
 50 //            char word=‘a‘;//BCL:System.Char
 51 //            /*string*/
 52 //            string strExample1="nihao";
 53 //            string strExample2="nihao";
 54 //            string strNull=null;
 55 //            if(strNull==null)
 56 //            {
 57 //                Console.WriteLine("strNull is Null");
 58 //            }
 59 //            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);
 60 //            Console.WriteLine(strExample1);
 61 //            Console.WriteLine(strExample2);
 62 //
 63 //            //Display "Ox2A"
 64 //            Console.WriteLine("0x{0:X}",42);
 65             //Fightable fight=(new Person("zhang")) as Fightable;//将Person类型转换为父接口Fightable
 66             //fight.Fight();
 67 //            Person p=new Person();
 68 //            p.person=new Person();
 69             //Person p=new Person("xiaonizi"){person=new Person("haoren")};
 70             //p.person.Fight();
 71 //            unchecked//强制不做溢出检查,checked做溢出检查
 72 //            {
 73 //                int maxNum=int.MaxValue;//2147483647
 74 //                maxNum+=1;
 75 //                Console.WriteLine(maxNum);
 76 //            }
 77             /*先声明后赋值*/
 78 //            int [] arrayNum1;
 79 //             arrayNum1=new int[3];//onw dismentional
 80 //            arrayNum1={11,22,33};//错误
 81 //             int[,] arrayNum2;
 82 //            arrayNum2=new int[2,3];//two dismentional
 83 //            int [,,] arrayNum3;
 84 //             arrayNum3=new int[2,3,4]; //three dismentional
 85 //            int [,] cells=int[3,3];
 86 //            cells={{1,2,3},{1,2,0},{1,2,1}};//错误
 87 //            arrayNum2={{1,2,3},{1,2,3}};//错误
 88             /*声明时赋值*/
 89 //            int [] arrayNum11={1,2,3};
 90 //            int [,] arrayNum22={{1,2},{1,2},{2,3}};
 91             int [,,] arrayNum33={{{11,23},{22,45}},{{33,67},{44,58}}};
 92 //            int[] arrayNum44=new int[]{0,1,2};
 93 //            int[] arrayNum55=new int[3]{1,2,3};//其实不同写多少个,但是一但写了之后,在后面的赋值就必须有固定个数,不能多也不能少
 94 //            foreach(int num in arrayNum33)
 95 //            {
 96 //                Console.WriteLine(num);
 97 //            }
 98 //            string[] strArray=new string[3];
 99 //            Console.WriteLine(strArray[0]);
100 //            int[] test=new int[3];
101 //            Console.WriteLine(test[1]);
102             //Console.WriteLine(arrayNum33[1,1,1]);//访问数组里面的元素
103             //Console.WriteLine("{0},{1},{2}",arrayNum1.Length,arrayNum2.Length,arrayNum3.Length);
104             /*交错数组:解析:int[]是数据类型,所以他之后的[]声明了一个int[]类型的数组,注意,交错数组要求为内部的每个数组都创建数组实例*/
105             int[][] cells={
106                 new int[]{1,0,2,0},
107                 new int[]{1,2,0},
108                 new int[]{1,2},
109                 new int[]{1}
110             };
111             cells[0][1]=3;//访问交错数组中的元素
112             Array.Reverse(cells);
113 //            Array.Clear(cells,0,cells.Length-2);
114 //            foreach(int num in cells)//会报错:无法将类型“int[]”转换为“int”也就是说明上面的一个数组的类型是int[],
115 //            {
116 //                Console.WriteLine(num);
117 //            }
118 //            foreach(int[] num in cells)//num遍历到的是每一个int数组,换句话说,就是cells里面的元素是数组
119 //            {
120 //                foreach(int subNum in num)//subNum表示的是每一个数组里面的元素
121 //                {
122 //                    Console.Write(" "+subNum+" ");
123 //                }
124 //                Console.WriteLine();
125 //            }
126             Console.ReadKey(true);
127         }
128     }
129     interface Fightable
130     {
131         void Fight();
132     }
133     class Person:Fightable
134     {
135         public Person(string name )
136         {
137             this.Name=name;
138         }
139         public Person person{get;set;}
140         public string Name{get;set;}
141         public void Fight()
142         {
143             System.Console.WriteLine("I can fight");
144         }
145     }
146 }

 

C# 数据类型 数组,引用类型 值类型

标签:

原文地址:http://www.cnblogs.com/vivieryuan/p/4242879.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!