数组是一种数据结构,它包含若干相同类型的变量。数组是使用类型声明的:
type[] arrayName;
下面的示例创建一维、多维和交错数组:
| C# | 复制代码 |
|---|---|
class TestArraysClass
{
static void Main()
{
// Declare a single-dimensional array
int[] array1 = new int[5];
// Declare and set array element values
int[] array2 = new int[] { 1, 3, 5, 7, 9 };
// Alternative syntax
int[] array3 = { 1, 2, 3, 4, 5, 6 };
// Declare a two dimensional array
int[,] multiDimensionalArray1 = new int[2, 3];
// Declare and set array element values
int[,] multiDimensionalArray2 = { { 1, 2, 3 }, { 4, 5, 6 } };
// Declare a jagged array
int[][] jaggedArray = new int[6][];
// Set the values of the first array in the jagged array structure
jaggedArray[0] = new int[4] { 1, 2, 3, 4 };
}
}
| |
数组概述
数组具有以下属性:
相关章节
C# 语言规范
有关更多信息,请参见 C# 语言规范中的以下各章节:
-
1.8 数组
-
12 数组