C# string.split 返回一个 string[] 我想要一个 List<string> 是否有一个班轮将数组转换为列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/251924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:00:20  来源:igfitidea点击:

string.split returns a string[] I want a List<string> is there a one liner to convert an array to a list?

c#arraysgenericslist

提问by minty

Lists in C# have the .ToArray()method. I want the inverse, where an array is transformed into a list. I know how to create a list and loop through it but I would like a one liner to swap it back.

C# 中的列表具有该.ToArray()方法。我想要相反的,其中一个数组被转换成一个列表。我知道如何创建一个列表并循环遍历它,但我想要一个班轮将其交换回来。

I am using the String.Splitmethod in the .NET 2.0 environment, so LINQ, etc. is not available to me.

String.Split在 .NET 2.0 环境中使用该方法,所以 LINQ 等对我不可用。

采纳答案by Ovidiu Pacurar

string s = ...
new List<string>(s.Split(....));

回答by Ty.

return new List<string>(stringArray);

回答by Max Lybbert

In .Net 3.5, the System.Linqnamespace includes an extension method called ToList<>().

在 .Net 3.5 中,System.Linq命名空间包含一个名为ToList<>().

回答by Peter van der Heijden

If all you need is an object that implements the IList interface and you do not need to add new items you might also do it like this:

如果您只需要一个实现 IList 接口的对象并且您不需要添加新项目,您也可以这样做:

IList<string> list = myString.Split(' ');