如何在 C Sharp 中定义一个单维数组?
数组用于存储数据集合,但它通常更像是将同类型的一组变量存储在连续的内存位置。
要定义一维数组−
int[] runs = new int[10];
现在让我们在同一行中初始化数组−
int[] runs = new int[5] {125, 173, 190, 264, 188};
以下是一个示例,演示如何声明、初始化和显示数组−
示例
using System; namespace Program { class Demo { static void Main(string[] args) { int[] runs = new int[5] {125, 173, 190, 264, 188}; int i,j; for (j = 0; j < 5; j++ ) { Console.WriteLine("Innings score of Cricketer[{0}] = {1}", j, runs[j]); } Console.ReadKey(); } } }
结果
Innings score of Cricketer[0] = 125 Innings score of Cricketer[1] = 173 Innings score of Cricketer[2] = 190 Innings score of Cricketer[3] = 264 Innings score of Cricketer[4] = 188
广告