内容提要
1.数组的简介
2.如何创建和引用一维数组
C#里面的数组是一个类,我们声明一个数组的时候实际上是声明了一个Array类型的对象,所以在实例化的时候会使用new关键字,因为是对象所以数组本身就是引用类型的了。数组一般分为一维数组、多维数组和交错数组,这里主要说一维数组,我个人觉得把一维数组看透了,另外两种那就是手到擒来了。
C#里面基本沿用了C/C++里面定义数组的方式,虽然他是一个对象但我们不能像这样声明Array myarray = new Array(),Array被定义成了一个abstract类,并且没有公共的构造函数。数组具体的定义方法如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
初始化数组,上面的两个例子其实已经初始化了数组,new运算符把他们初始化为默认值,还可以以下面的方式来初始化:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
数组的调用方法也很简单,但要注意的是,调用时使用的下标是从0开始,这样数组的最大下标就比数组的长度小1。
![](/Images/OutliningIndicators/None.gif)
最后还要说以下数组是引用类型的问题,不管你创建的是整型数组还是字符串数组,那么他在做为参数传递给一个方法时,在方法里对数组的如何改动都会影响到传入的数组,具体如下:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)