Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

arrays

c#

list

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());      }  }  
like image 290
Colin Roe Avatar asked Oct 14 '12 13:10

Colin Roe


People also ask

Can you add an array to a list C#?

Use the Add() method or object initializer syntax to add elements in an ArrayList . An ArrayList can contain multiple null and duplicate values. Use the AddRange(ICollection c) method to add an entire Array, HashTable, SortedList, ArrayList , BitArray , Queue, and Stack in the ArrayList .

Can I put string in array in C?

First Case, take input values as scanf("%s", input[0]); , similarly for input[1] and input[2] . Remember you can store a string of max size 10 (including '\0' character) in each input[i] . In second case, get input the same way as above, but allocate memory to each pointer input[i] using malloc before.

Can you add a string to an array?

You can also add an element to String array by using ArrayList as an intermediate data structure. An example is shown below. As shown in the above program, the string array is first converted into an ArrayList using the asList method. Then the new element is added to the ArrayList using the add method.


1 Answers

Use List.AddRange instead of List.Add

like image 166
Claus Jørgensen Avatar answered Oct 07 '22 19:10

Claus Jørgensen