How do you expect List<EmailData>.Add
to know how to turn three string
s into an instance of EmailData
? You're expecting too much of the Framework. There is no overload of List<T>.Add
that takes in three string parameters. In fact, the only overload of List<T>.Add
takes in a T
. Therefore, you have to create an instance of EmailData
and pass that to List<T>.Add
. That is what the above code does.
Try:
lstemail.Add(new EmailData {
FirstName = "JOhn",
LastName = "Smith",
Location = "Los Angeles"
});
This uses the C# object initialization syntax. Alternatively, you can add a constructor to your class
public EmailData(string firstName, string lastName, string location) {
this.FirstName = firstName;
this.LastName = lastName;
this.Location = location;
}
Then:
lstemail.Add(new EmailData("JOhn", "Smith", "Los Angeles"));