C#中如何宣告陣列?
要在 C# 中宣告一個數組,可以使用以下語法 −
datatype[ ] 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP