将数据存储到列表中

我有以下class级:

public class EmailData { public string FirstName{ set; get; } public string LastName { set; get; } public string Location{ set; get; } } 

然后我做了以下,但没有正常工作:

 List<EmailData> lstemail = new List<EmailData>(); lstemail.Add("JOhn","Smith","Los Angeles"); 

我得到一个消息,说没有超载的方法需要3个参数。

如果你想实例化和添加在同一行,你必须做这样的事情:

 lstemail.Add(new EmailData { FirstName = "JOhn", LastName = "Smith", Location = "Los Angeles" }); 

或者只是先实例化对象,然后直接将其添加到:

 EmailData data = new EmailData(); data.FirstName = "JOhn"; data.LastName = "Smith"; data.Location = "Los Angeles" lstemail.Add(data); 

你需要添加一个类的实例:

 lstemail.Add(new EmailData { FirstName = "John", LastName = "Smith", Location = "Los Angeles"}); 

我会build议你的类添加一个构造函数,但是:

 public class EmailData { public EmailData(string firstName, string lastName, string location) { this.FirstName = firstName; this.LastName = lastName; this.Location = location; } public string FirstName{ set; get; } public string LastName { set; get; } public string Location{ set; get; } } 

这将允许您使用构造函数将添加项添加到列表中:

 lstemail.Add(new EmailData("John", "Smith", "Los Angeles")); 

您需要新build一个EmailData实例,然后添加:

 var data = new EmailData { FirstName = "John", LastName = "Smith", Location = "LA" }; List<EmailData> listemail = new List<EmailData>(); listemail.Add(data); 

如果你想能够做到:

 listemail.Add("JOhn","Smith","Los Angeles"); 

你可以创build你自己的自定义列表,通过专门化System.Collections.Generic.List和实现你自己的Add方法,或多或less像这样:

 public class EmailList : List<EmailData> { public void Add(string firstName, string lastName, string location) { var data = new EmailData { FirstName = firstName, LastName = lastName, Location = location }; this.Add(data); } } 

一种方法(一行)就是这样的:

 listemail.Add(new EmailData {FirstName = "John", LastName = "Smith", Location = "Los Angeles"}); 

您需要创build要添加的类的实例:

 lstemail.Add(new EmailData { FirstName = "JOhn", LastName = "Smith", Location = "Los Angeles" }); 

请参阅如何:使用对象初始值设定项初始化对象(C#编程指南)


或者,您可以为您的EmailData对象声明一个构造函数,并使用它来创build该实例。

您不会将该类的新实例添加到列表中。 尝试这个:

 lstemail.Add(new EmailData { FirstName="John", LastName="Smith", Location="Los Angeles" });` 

List是一个通用的类。 当您指定一个List<EmailData>Add方法需要一个EmailDatatypes的EmailData 。 上面的例子用更详细的语法来表示:

 EmailData data = new EmailData(); data.FirstName="John"; data.LastName="Smith; data.Location = "Los Angeles"; lstemail.Add(data); 

你正试图打电话

 列表<EmailData>。新增(string,string,string) 

。 尝试这个:

 lstemail.add(new EmailData{ FirstName="John", LastName="Smith", Location="Los Angeles"}); 

你如何期望List<EmailData>.Add知道如何将三个string变成一个EmailData的实例? 你期望太多的框架。 List<T>.Add没有重载,它List<T>.Add三个string参数。 实际上, List<T>.Add的唯一重载就是T 因此,您必须创build一个EmailData的实例,并将其传递给List<T>.Add 。 这就是上面的代码所做的。

尝试:

 lstemail.Add(new EmailData { FirstName = "JOhn", LastName = "Smith", Location = "Los Angeles" }); 

这使用C#对象初始化语法。 另外,你可以添加一个构造函数到你的类中

 public EmailData(string firstName, string lastName, string location) { this.FirstName = firstName; this.LastName = lastName; this.Location = location; } 

然后:

 lstemail.Add(new EmailData("JOhn", "Smith", "Los Angeles")); 

这条线是你的问题:

 lstemail.Add("JOhn","Smith","Los Angeles"); 

没有从3个string直接投射到您的自定义类。 编译器没有办法弄清楚你想用这一行做什么。 你需要Add()一个类的实例到lstemail

 lstemail.Add(new EmailData { FirstName = "JOhn", LastName = "Smith", Location = "Los Angeles" }); 

这是扩展方法版本:

 public static class ListOfEmailDataExtension { public static void Add(this List<EmailData> list, string firstName, string lastName, string location) { if (null == list) throw new NullReferenceException(); var emailData = new EmailData { FirstName = firstName, LastName = lastName, Location = location }; list.Add(emailData); } } 

用法:

 List<EmailData> myList = new List<EmailData>(); myList.Add("Ron", "Klein", "Israel"); 

如果你想创build一些元素的列表开始:

 var emailList = new List<EmailData> { new EmailData { FirstName = "John", LastName = "Doe", Location = "Moscow" }, new EmailData {.......} }; 
  public IEnumerable<CustInfo> SaveCustdata(CustInfo cust) { try { var customerinfo = new CustInfo { Name = cust.Name, AccountNo = cust.AccountNo, Address = cust.Address }; List<CustInfo> custlist = new List<CustInfo>(); custlist.Add(customerinfo); return custlist; } catch (Exception) { return null; } } 
 EmailData clsEmailData = new EmailData(); List<EmailData> lstemail = new List<EmailData>(); clsEmailData.FirstName="JOhn"; clsEmailData.LastName ="Smith"; clsEmailData.Location ="Los Angeles" lstemail.add(clsEmailData);