在C#中,可以使用以下方式定义和赋值数组:
- 使用数组初始化器:
int[] numbers = { 1, 2, 3, 4, 5 };
- 使用new关键字和数组大小进行定义:
int[] numbers = new int[5];
然后使用索引进行赋值:
numbers[0] = 1; numbers[1] = 2; numbers[2] = 3; numbers[3] = 4; numbers[4] = 5;
- 使用new关键字和初始化数组元素的值进行定义:
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
注意:数组的索引从0开始,所以在使用索引进行赋值时,需要注意索引的范围。