温馨提示×

c#如何定义数组

c#
小亿
81
2024-01-13 06:06:11
栏目: 编程语言

在C#中,可以通过以下方式来定义数组:

  1. 使用数组初始化器(Array Initializer):
int[] numbers = { 1, 2, 3, 4, 5 };
  1. 通过new关键字和数组类型来创建一个数组:
int[] numbers = new int[5];
  1. 使用索引方式初始化数组元素:
int[] numbers = new int[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;

需要注意的是,数组的索引是从0开始的。上述代码中,numbers数组的长度为5,可以存储5个整数。

0