Colin Roe
Colin Roe

Reputation: 804

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

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.

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()); 
    } 
} 

Upvotes: 48

Views: 129869

Answers (3)

Claus Jørgensen
Claus Jørgensen

Reputation: 26345

Use List.AddRange instead of List.Add

Upvotes: 84

Adil
Adil

Reputation: 148180

Use List.AddRange instead of List.Add

Change

 wrd.Add(_lineParts.ToString());

To

wrd.AddRange(_lineParts);

Upvotes: 27

Rahul Tripathi
Rahul Tripathi

Reputation: 172628

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

Upvotes: 5

Related Questions