数组用于存储数据集合。它存储相同类型元素的固定大小的顺序集合。
要声明一个数组,请遵循以下给定的语法-
datatype[] arrayName;
这里,
数据类型用于指定数组中元素的类型。
[]设置数组的等级。等级指定数组的大小。
arrayName指定数组的名称。
现在让我们看一个例子-
int[] goals;
以下是显示如何在C#中声明和初始化数组的示例。
using System;
namespace Demo {
class MyArray {
static void Main(string[] args) {
int [] goals = new int[5] {3,2,1,5,4};
int i,j;
for (j = 0; j < 5; j++ ) {
Console.WriteLine("Goals in FIFA - Match[{0}] = {1}", j, goals[j]);
}
Console.ReadKey();
}
}
}输出结果
Goals in FIFA - Match[0] = 3 Goals in FIFA - Match[1] = 2 Goals in FIFA - Match[2] = 1 Goals in FIFA - Match[3] = 5 Goals in FIFA - Match[4] = 4