C# Array Definition and Assignment Guide
在C#中,定义数组并赋值的方法有以下几种:
- Initialize the array using an array initializer.
// 定义并赋值一个整数数组
int[] numbers = { 1, 2, 3, 4, 5 };
// 定义并赋值一个字符串数组
string[] names = { "John", "Amy", "Tom" };
- fresh
// 定义一个长度为5的整数数组,并逐个赋值
int[] numbers = new int[5];
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;
numbers[3] = 4;
numbers[4] = 5;
// 定义一个长度为3的字符串数组,并逐个赋值
string[] names = new string[3];
names[0] = "John";
names[1] = "Amy";
names[2] = "Tom";
- fresh
// 定义并赋值一个整数数组
int[] numbers = new int[] { 1, 2, 3, 4, 5 };
// 定义并赋值一个字符串数组
string[] names = new string[] { "John", "Amy", "Tom" };
You can define and assign values to an array using any method.