C#學習筆記2
阿新 • • 發佈:2018-06-11
comm 保留 默認值 system AI sys In 命名空間 num
C#數組
當創建一個數組時,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();
}
C#學習筆記2