I had this code:
String[] lineElements;
. . .
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
lineElements = line.Split(',');
. . .
but then thought I should maybe go with a List instead. But this code:
List<String> listStrLineElements;
. . .
try
{
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
listStrLineElements = line.Split(',');
. . .
...gives me, "Cannot implicitly convert type 'string[]' to 'System.Collections.Generic.List'"
string.Split()
returns an array - you can convert it to a list using ToList()
:
listStrLineElements = line.Split(',').ToList();
Note that you need to import System.Linq
to access the .ToList()
function.