如何在C#中将多个值分配给同一变量?
要将多个值设置为同一变量,请在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
热门推荐
10 感谢韩语老师祝福语简短
11 升职文案高级祝福语简短
12 花童结婚祝福语大全简短
13 向国庆送祝福语简短
14 什么寄语简短祝福语女生
15 客户回礼祝福语大全简短
16 女孩生日祝福语简短创意
17 家长回复节日祝福语简短
18 给外婆敬酒祝福语简短