C# 中的陣列是否從 0 開始索引?
是的,C# 中的陣列從 0 開始索引。我們來看看如何實現 -
- 如果該陣列為空,則它沒有元素,長度為 0。
- 如果該陣列在 0 索引中有一個元素,則它的長度為 1。
- 如果該陣列在 0 和 1 索引中有兩個元素,則它的長度為 2。
- 如果該陣列在 0、1 和 2 索引中有三個元素,則它的長度為 3。
以下說明 C# 中的陣列從索引 0 開始 -
/* begin from index 0 */
for ( i = 0; i < 5; i++ ) {
n[ i ] = i + 5;
}示例
你可以嘗試執行以下程式碼,看看陣列索引如何在 C# 中實現 -
using System;
namespace ArrayApplication {
class MyArray {
static void Main(string[] args) {
int [] n = new int[5];
int i,j;
/* begings from index 0 */
for ( i = 0; i < 5; i++ ) {
n[ i ] = i + 5;
}
for (j = 0; j < 5; j++ ) {
Console.WriteLine("Element[{0}] = {1}", j, n[j]);
}
Console.ReadKey();
}
}
}結果
Element[0] = 5 Element[1] = 6 Element[2] = 7 Element[3] = 8 Element[4] = 9
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP