温馨提示×

c#数组怎么定义和赋值

c#
小亿
134
2023-08-18 02:40:27
栏目: 编程语言

在C#中,可以使用以下方式定义和赋值数组:

  1. 使用数组初始化器:
int[] numbers = { 1, 2, 3, 4, 5 };
  1. 使用new关键字和数组大小进行定义:
int[] numbers = new int[5];

然后使用索引进行赋值:

numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
  1. 使用new关键字和初始化数组元素的值进行定义:
int[] numbers = new int[] { 1, 2, 3, 4, 5 };

注意:数组的索引从0开始,所以在使用索引进行赋值时,需要注意索引的范围。

0