当创建一个数组时,C# 编译器会根据数组类型隐式初始化每个数组元素为一个默认值。例如,int 数组的所有元素都会被初始化为 0。
Array类在 System 命名空间中定义,是所有数组的基类
C#中的数组主要保留三种类型:
1. 一维数组
2. 多维数组(最简单的就是二维数组)
3. 交错数组(即数组的数组)
数组的声明示例如下:
public static void Main()
{
int[] a = { 1, 2, 3, 4, 5 };
int[,] b = { { 1, 2 }, { 3, 4 } };
int[][] c = { new int[] { 1, 2, 3, 4 }, new int[] { 5, 6 }, new[] { 7 }, new int[3] { 1, 4, 2 } };
int[] n = new int[10];
//initialization
for (int i = 0; i < n.Length; i++)
n[i] = i;
Console.WriteLine("访问a中的元素:");
foreach (int x in a)
{
Console.WriteLine(x);
}
Console.WriteLine("访问c中的元素:");
foreach (int[] x in c)
{
Console.WriteLine(x);
for (int i = 0; i < x.Length; i++)
Console.WriteLine(x[i]);
}
Console.ReadKey();
}
原文:https://www.cnblogs.com/xiaomeizai/p/9165014.html