首页 > 解决方案 > 将字符串 [] 转换为列表在c#中

问题描述

我一直在做一个小项目,我遇到了这个问题。我有一个充满行的 txt 文件,我需要将它们存储在一个列表中。有什么优雅的方法吗?这是我的代码,但是,它不会起作用,因为有些东西没有绑定。txt 文件有 126 行,但我只需要其中的 125 行。感谢您的时间,任何帮助表示赞赏:)

string[] Number = System.IO.File.ReadAllLines("Numbers.txt");
List<string> listNumbers = new List<string>(); //place where all numbers will be stored
for (int i = 0; i<125; i++)
{
    listNumbers[i] = Number[i];
}

标签: c#

解决方案


只需致电ToList()

myArray.ToList();

或者:

var list = new List<string>(myArray);

推荐阅读