聲明同時指定維數, 但暫不賦值:
using System;
class MyClass
{
static void Main()
{
int[] arr = new int[3];
foreach (int i in arr) Console.WriteLine(i); // 0/0/0
arr[0] = 11;
arr[1] = 22;
arr[2] = 33;
foreach (int i in arr) Console.WriteLine(i); // 11/22/33
Console.ReadKey();
}
}
先聲明, 賦值時再確定維數:
using System;
class MyClass
{
static void Main()
{
int[] arr;
arr = new int[] { 11, 22, 33 };
foreach (int i in arr) Console.WriteLine(i); // 11/22/33
Console.ReadKey();
}
}
可改變聲明時的維數:
using System;
class MyClass
{
static void Main()
{
int[] arr = new int[3];
arr = new int[4] { 11, 22, 33, 44 };
foreach (int i in arr) Console.WriteLine(i); // 11/22/33/44
Console.ReadKey();
}
}