如何在 C# 中为同一个变量分配多个值?\
csharpprogrammingserver side programming更新于 2025/5/30 3:07:17
要为同一个变量设置多个值,请在 C# 中使用数组。假设不是使用 5 个变量,而是使用单个变量中的数组设置这 5 个变量。
以下是使用字符串数组 − 将三个值设置为单个变量的示例
string[] arr = new string[3];
现在让我们初始化它 −
string[] arr = new string[3] {"one", "two", "three"};
以下是完整的示例 −
示例
using System;
public class Demo {
static void Main(string[] args) {
string[] arr = new string[3] {"one", "two", "three"};
for (int i = 0; i < arr.Length; i++) {
Console.WriteLine("Values: " + arr[i]);
}
Console.ReadKey();
}
}
输出
Values: one Values: two Values: three

