C# 语法练习(7): 数组
2009-02-23 08:16:57 来源:WEB开发网字符串数组:
using System;
class MyClass
{
static void Main()
{
string[] arr = new string[3] { "aa", "bb", "cc" };
foreach (string s in arr) Console.WriteLine(s); // aa/bb/cc
Console.ReadKey();
}
}
整数数组:
using System;
class MyClass
{
static void Main()
{
int[] arr = { 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] { 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];
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();
}
}
更多精彩
赞助商链接