如何在C#中声明数组?
要在C#中声明数组,可以使用以下语法-
datatype[ ] Name_of_array;
这里,
数据类型用于指定数组中元素的类型。
[]指定数组的大小。
Name_of_array指定数组的名称。
以下是一个例子-
double[ ] balance;
让我们看一个示例,其中声明了一个数组并将值添加到其中-
示例
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
int [ ] n = new int[10]; /* n is an array of 10 integers */
int i,j;
/* initialize elements of array n */
for ( i = 0; i < 10; i++ ) {
n[ i ] = i + 100;
}
/* output each array element's value */
for (j = 0; j < 10; j++ ) {
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
Console.ReadKey();
}
}
}输出结果
Element[0] = 100 Element[1] = 101 Element[2] = 102 Element[3] = 103 Element[4] = 104 Element[5] = 105 Element[6] = 106 Element[7] = 107 Element[8] = 108 Element[9] = 109
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短