I am trying to read a list of contacts from a CSV file into a list.
My code below reads the file, and creates the list; however I am having problems accessing the elements from the list, ie values.Email etc...
namespace Contacts
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
ReadContacts();
}
static void ReadContacts()
{
List<Contacts> values = File.ReadAllLines("D:\\Contacts.csv")
.Skip(1)
.Select(v => Contacts.FromCsv(v))
.ToList();
Debug.WriteLine(values[0].Email);
}
}
class Contacts
{
string First;
string Last;
string Email;
public static Contacts FromCsv(string csvLine)
{
string[] values = csvLine.Split(',');
Contacts contacts = new Contacts();
contacts.First = values[0];
contacts.Last = values[1];
contacts.Email = values[2];
return contacts;
}
}
}