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

c# list转数组

作者:清梦独行   发布日期:2026-04-15   浏览:80

// C# List 转数组的示例代码

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // 创建一个 List 并添加一些元素
        List<int> numberList = new List<int>();
        numberList.Add(1);
        numberList.Add(2);
        numberList.Add(3);
        numberList.Add(4);
        numberList.Add(5);

        // 将 List 转换为数组
        int[] numberArray = numberList.ToArray();

        // 输出数组中的元素以验证转换是否成功
        Console.WriteLine("转换后的数组元素:");
        foreach (int num in numberArray)
        {
            Console.WriteLine(num);
        }
    }
}

解释说明:

  1. 创建 List:首先我们创建了一个 List<int> 类型的列表,并向其中添加了一些整数元素。
  2. 转换为数组:使用 ToArray() 方法将 List<int> 转换为 int[] 数组。ToArray()List<T> 类提供的一个方法,它会返回一个新的数组,该数组包含 List 中的所有元素。
  3. 输出结果:最后,通过遍历数组并打印每个元素来验证转换是否成功。

这个例子展示了如何轻松地将 List 转换为数组,并且可以用于任何类型的 List(不仅仅是 int)。

上一篇:c#post

下一篇:c# 静态变量

大家都在看

c# 二进制

c# 创建目录

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

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

Laravel 中文站