将字符串数组 (string[]) 添加到 List<string> c#

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

Adding string array (string[]) to List<string> c#

c#arrayslist

提问by Colin Roe

When the string[], _lineParts is added to the List, all I see in the List is "System.String[]" What needs to be done to see the actually string[] values in the list.

当string[], _lineParts 添加到List 时,我在List 中看到的只是“System.String[]” 需要做什么才能看到列表中的实际string[] 值。

while (_aLine != null) 
{ 
    //split the line read into parts delimited by commas 
    _lineParts = _aLine.Split(new char[] { ' ', '\u000A', ',', '.', ';', ':', '-', '_', '/' }, 
        StringSplitOptions.RemoveEmptyEntries); 
    //keep things going by reading the next  line 
    _aLine = sr.ReadLine(); 
    //words = _lineParts; 
    if (_lineParts != null) 
    { 
        //_words.Add(_lineParts.ToString()); 
        wrd.Add(_lineParts.ToString()); 
    } 
} 

采纳答案by Claus J?rgensen

Use List.AddRangeinstead of List.Add

使用List.AddRange而不是 List.Add

回答by Rahul Tripathi

You can use List.AddRange()where you are using List.Add()

您可以List.AddRange()在您使用的地方使用List.Add()

回答by Adil

Use List.AddRangeinstead of List.Add

使用List.AddRange而不是List.Add

Change

改变

 wrd.Add(_lineParts.ToString());

To

wrd.AddRange(_lineParts);