c# - 将字符串 [] 转换为列表在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];
}
解决方案
只需致电ToList()
:
myArray.ToList();
或者:
var list = new List<string>(myArray);
推荐阅读
- javascript - 单例不能跨多个文件Nodejs工作
- javascript - 获取给定数字周围的数字范围
- c# - 为什么整数运算结果在 VB.NET 和 C# 中显示不同的值?
- python - 如何从 jvm 导入 Scala 案例对象?
- networking - 为什么 NSE 的网站不响应来自 GCP VM 的 GET 请求,而是响应来自本地机器的请求?
- javascript - 在 React 中渲染和显示对象数组的正确方法
- swift - 如何在 xcode 中的地图上显示注释?
- javascript - 获取数组内的对象
- java - Gradle 无法从 nexus 存储库获取依赖项?
- permissions - 为什么 lxc 命令给了我权限被拒绝的错误?