C# 从数组填充列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/703762/
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-04 14:05:33  来源:igfitidea点击:

Populate list from array

c#arraysgenerics

提问by leora

if i have an array. can i populate a generic list from that array:

如果我有一个数组。我可以从该数组中填充一个通用列表:

Foo[] fooList . . . (assume populated array)

// This doesn't seem to work
List<Foo> newList = new List<Foo>(fooList);

采纳答案by Andy White

You could convert the array to a List:

您可以将数组转换为列表:

string[] strings = { "hello", "world" };
IList<string> stringList = strings.ToList();

回答by kaivalya

You are looking for List(t).AddRangeMethod

您正在寻找List(t).AddRange方法

回答by Matt Hamilton

As @korki said, AddRange will work, but the code you've posted should work fine. For example, this compiles:

正如@korki 所说,AddRange 可以工作,但您发布的代码应该可以正常工作。例如,这编译:

var i = new int[10];
var list = new List<int>(i);

Could you show us more of your code?

你能向我们展示更多你的代码吗?