I have written a small program to read data from a csv file:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace test { class Program { static void Main( string[] args ) { var reader = new StreamReader( File.OpenRead( @"C:\Users\Desktop\Results.csv" ) ); List<string> listA = new List<string>(); List<string> listB = new List<string>(); while ( !reader.EndOfStream ) { var line = reader.ReadLine(); var values = line.Split( ',' ); listA.Add( values[0] ); listB.Add( values[1] ); } // Print column one. Console.WriteLine( "Column 1:" ); foreach ( var element in listA ) Console.WriteLine( element ); // Print column two. Console.WriteLine( "Column 2:" ); foreach ( var element in listB ) Console.WriteLine( element ); Console.ReadKey(); } } } I get the following error message on line listB.Add( values[1] );
Index was outside the bounds of the array. When I comment out everything to do with listB, it works and shows me the 1st column...
Could someone please help me understand what I am doing wrong?
Thank you,
values? Is it being populated with anything? What is the string it should have been populated with?