如何在 C# 中向同一变量分配多个值?\n
如需向同一变量设置多个值,请在 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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP