C# 数组从零开始建立索引,即数组索引从零开始。C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似。但还有一些差异应引起注意。
声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面。在 C# 中,将方括号放在标识符后是不合法的语法。
int[] table; // not int table[];
int[] numbers; // declare numbers as an int array of any size
numbers = new int[10]; // numbers is a 10-element array
numbers = new int[20]; // now it's a 20-element array
//一维数组
int[] numbers = new int[5] {1, 2, 3, 4, 5};
string[] names = new string[3] {"Matt", "Joanne", "Robert"};
/********* 可省略数组的大小,如下所示: *********/
int[] numbers = new int[] {1, 2, 3, 4, 5};
string[] names = new string[] {"Matt", "Joanne", "Robert"};
/** 如果提供了初始值设定项,则还可以省略 new 运算符,如下所示: **/
int[] numbers = {1, 2, 3, 4, 5};
string[] names = {"Matt", "Joanne", "Robert"};
//多维数组
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
/*** 可省略数组的大小,如下所示: ***/
int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} };
/** 如果提供了初始值设定项,则还可以省略 new 运算符,如下所示: **/
int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };
//交错的数组(数组的数组)
//可以像下例所示那样初始化交错的数组:
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
//可省略第一个数组的大小,如下所示:
int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
//-或-
int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
//请注意,对于交错数组的元素没有初始化语法。
//声明一维数组,没有初始化,等于null
int[] intArray1;
//初始化已声明的一维数组
intArray1 = new int[3];
intArray1 = new int[3]{1,2,3};
intArray1 = new int[]{1,2,3};
//声明一维数组,同时初始化
int[] intArray2 = new int[3]{1,2,3};
int[] intArray3 = new int[]{4,3,2,1};
int[] intArray4 = {1,2,3,4};
string[] strArray1 = new string[]{"One","Two","Three"};
string[] strArray2 = {"This","is","an","string","Array"};
//声明二维数组,没有初始化
short[,] sArray1;
//初始化已声明的二维数组
sArray1 = new short[2,2];
sArray1 = new short[2,2]{{1,1},{2,2}};
sArray1 = new short[,]{{1,2,3},{4,5,6}};
//声明二维数组,同时初始化
short[,] sArray2 = new short [1,1]{{100}};
short[,] sArray3 = new short [,]{{1,2},{3,4},{5,6}};
short[,] sArray4 = {{1,1,1},{2,2,2}};
//声明三维数组,同时初始化
byte[,,] bArray1 = {{{1,2},{3,4}},{{5,6},{7,8}}};
//声明交错数组,没有初始化
int[][] JagIntArray1;
//初始化已声明的交错数组
JagIntArray1 = new int [2][] {
new int[]{1,2},
new int[]{3,4,5,6}
};
JagIntArray1 = new int [][]{
new int[]{1,2},
new int []{3,4,5},
intArray2 //使用int[]数组变量
};
//声明交错数组,同时初始化
int[][] JagIntArray2 = {
new int[]{1,1,1},
new int []{2,2},
intArray1
};
分享到:
相关推荐
C# 数组与序列化C# 数组与序列化C# 数组与序列化C# 数组与序列化C# 数组与序列化C# 数组与序列化C# 数组与序列化
C#数组 结构 枚举
C# 数组 查找 排序
asp.net中后台c#数组与前台js数组交互上述代码即为我解决问题所用代码,均已试验通过。
关于C#数组,有一维,二维的等等。主要是数据的基础知识。
c#数组索引器集合典型程序示例,简单易学。
C#数组集合C#数组集合C#数组集合C#数组集合C#数组集合C#数组集合
C#数组元素是否重复,本人在项目中用过很OK
C# 数组从零开始建立索引,即数组索引从零开始。C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似。但还有一些差异应引起注意。 声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面。在 C# 中...
c# 数组
简单的C#倒序输出C#使用教程电子工业出版社出版课后习题
主要介绍了C#实现将数组内元素打乱顺序的方法,涉及C#数组遍历及随机数操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
温故而知新可以为师矣,牢固的基础能支撑我们前进的更远!
C#数组的随机排序源码C#数组的随机排序源码C#数组的随机排序源码
将两个数组连接在一起,很简单的一个例子。
将数组转换为List 将List转换为数组 将数组转换为Dictionary 将Dictionary 转换为数组 将List转换为Dictionary 将Dictionary转换为List 首先这里定义了一个“Student”的类,它有三个自动实现属性。 class ...
C#数组的查找实例源码 int find(int[] array, int value) { if (array==null || array.Length==0) { return -1; } else { int index = 0; for (; index ; index++) { if (value == array[index]) { ...
c# 数组与集合(ArrayList)游戏开发高级使用举例 一、数组 定义:数组是相同数据类型的元素按一定顺序排列的集合。 优点:数组在存储数据的时候是连续存储的,因此可以快速而容易地从头到尾遍历元素,可以快速的...
C# 将两个整型数组的元素相加。别忘给评分!
将文本文件读入c#数组 并排序