C# 如何将数组的字符串复制到列表<string>?

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

How to copy string of array to list<string>?

c#stringwinformslist

提问by rangasathish

Possible Duplicate:
Convert array of strings to List<string>

可能的重复:
将字符串数组转换为 List<string>

I am new to C# How to copy entire string of array to List?

我是 C# 新手如何将整个数组字符串复制到 List?

i have tried this but i didn't get any solution.

我试过这个,但我没有得到任何解决方案。

 List<string> lstArray  = strArray.toList<string>;

    or           
 List<string> lstArray = new List<string>();
 strArray.copyTo(lstArray,0);

采纳答案by Habib

Pass string array in list constructor.

在列表构造函数中传递字符串数组。

List<string> yourList = new List<string>(strArray);

The reason your first line didn't work was because you are not using the correct syntax. So instead of

您的第一行不起作用的原因是您没有使用正确的语法。所以代替

List<string> lstArray  = strArray.toList<string>;

Use

List<string> lstArray = strArray.ToList<string>();

or

或者

List<string> lstArray = strArray.ToList(); // with less keystrokes, since the array is of type string. 

For the 2nd option where you are trying to use Array.CopyTo, it works with array types, not generic list. You are probably getting the error.

对于您尝试使用 Array.CopyTo 的第二个选项,它适用于数组类型,而不是通用列表。您可能会收到错误消息。

The best overloaded method match for 'System.Array.CopyTo(System.Array, int)' has some invalid arguments

'System.Array.CopyTo(System.Array, int)' 的最佳重载方法匹配有一些无效参数

Since it expects an array and you are passing a generic list.

因为它需要一个数组,而您正在传递一个通用列表。