使用类将数据存储到列表中

时间:2011-12-05 21:07:43

标签: c# .net list

我有以下课程:

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个参数。

13 个答案:

答案 0 :(得分:80)

您需要添加该类的实例:

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

我建议您在类中添加构造函数,但是:

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"));

答案 1 :(得分:72)

如果要实例化并添加到同一行,则必须执行以下操作:

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);

答案 2 :(得分:29)

您需要新建一个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");

您可以通过专门化System.Collections.Generic.List并实现自己的Add方法来创建自己的自定义列表,或多或少像这样:

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);
    }
}

答案 3 :(得分:14)

这样做的一种方式(一行)是这样的:

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

答案 4 :(得分:13)

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

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

请参阅How to: Initialize Objects by Using an Object Initializer (C# Programming Guide)


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

答案 5 :(得分:8)

您没有将该类的新实例添加到列表中。试试这个:

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

List是一个通用类。指定List<EmailData>时,Add方法期望的对象类型为EmailData。以更详细的语法表达的上述示例将是:

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

答案 6 :(得分:8)

您正试图致电

List<EmailData>.Add(string,string,string)
。试试这个:

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

答案 7 :(得分:6)

您如何期望List<EmailData>.Add知道如何将三个string转换为EmailData的实例?你期待太多的框架。 List<T>.Add没有重载,它接收三个字符串参数。事实上,List<T>.Add的唯一重载是T。因此,您必须创建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"));

答案 8 :(得分:6)

这一行是你的问题:

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

没有从3个字符串直接转换到您的自定义类。编译器无法弄清楚你要用这条线做什么。您需要Add()类的实例lstemail

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

答案 9 :(得分:6)

这是扩展方法版本:

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");

答案 10 :(得分:2)

如果你想创建一个包含一些元素的列表:

var emailList = new List<EmailData>
{
   new EmailData { FirstName = "John", LastName = "Doe", Location = "Moscow" },
   new EmailData {.......}
};

答案 11 :(得分:1)

  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;
            }
        }

答案 12 :(得分:0)

EmailData clsEmailData = new EmailData();
List<EmailData> lstemail = new List<EmailData>(); 

clsEmailData.FirstName="JOhn";
clsEmailData.LastName ="Smith";
clsEmailData.Location ="Los Angeles"

lstemail.add(clsEmailData);