Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# 数组转list

作者:望断天涯   发布日期:2025-05-16   浏览:91

// 将数组转换为 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);
        }
    }
}

解释说明:

  1. 定义数组:首先我们定义了一个整数数组 array,其中包含了一些整数值。
  2. 转换为 List:使用 List<int> 的构造函数,并将数组作为参数传递给它,从而将数组转换为 List<int> 类型。
  3. 输出结果:通过 foreach 循环遍历并输出 List<int> 中的每个元素,以验证转换是否成功。

如果你需要将其他类型的数组转换为 List,只需更改类型参数即可。例如,对于字符串数组,可以使用 List<string>

上一篇:c#timer

下一篇:c# tablelayoutpanel

大家都在看

c# 二进制

c# tcp client

c# type.gettype

c# sqlconnection

.net和c#

c#游戏开发

c#网络编程

c# rectangle

c# if else

c# rtsp

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站