将字符串数组(string[])添加到List<string>中,使用C#编程语言。

46

当将字符串数组,_lineParts添加到列表时,列表中只显示"System.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()); 
    } 
} 

你没有将 _lineParts 添加到列表中,而是将 _lineParts.ToString() 添加到列表中。对于引用类型,ToString() 的默认行为是输出类型名称,这种情况下是 System.String[]。你是想将数组中的每个单独元素添加到列表中吗?还是想将整个数组作为单个元素添加到列表中? - David
将数组中的每个元素转换为列表。 - Colin Roe
3个回答

81

26

不错 - 突然我想起为什么我喜欢 JavaScript :-P - jebbie

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接