// 将数组转换为 List 的示例代码
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// 定义一个整数数组
int[] array = { 1, 2, 3, 4, 5 };
// 使用 List<T> 构造函数将数组转换为 List
List<int> list = new List<int>(array);
// 输出 List 中的元素以验证转换结果
foreach (int item in list)
{
Console.WriteLine(item);
}
}
}
array
,其中包含了一些整数值。List<int>
的构造函数,并将数组作为参数传递给它,从而将数组转换为 List<int>
类型。foreach
循环遍历并输出 List<int>
中的每个元素,以验证转换是否成功。如果你需要将其他类型的数组转换为 List
,只需更改类型参数即可。例如,对于字符串数组,可以使用 List<string>
。
上一篇:c#timer
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站