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