62

I have the following class:

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

I then did the following but was not working properly:

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

I get a message that says no overload for method takes 3 arguments.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Nate Pet
  • 44,246
  • 124
  • 269
  • 414

13 Answers13

87

You need to add an instance of the class:

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

I would recommend adding a constructor to your class, however:

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

This would allow you to write the addition to your list using the constructor:

lstemail.Add(new EmailData("John", "Smith", "Los Angeles"));
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I wouldn't recommend that at all! Why not simply use the Object Initialization syntax!?!?!? Unless you have logic in the constructor, or need to force certain parameters to be set, I wouldn't add a constructor. – John B Dec 09 '11 at 17:19
  • 5
    @JohnBubriski It's unlikely that there is a valid meaning for an `EmailData` class (in this instance) without a name or location. Using a default constructor would allow a user to inadvertently create a class that is in an invalid state. Whenever possible, it's ALWAYS a good idea to force constructors to include all required data to initialize the object into a meaningful state, as this will prevent errors from occurring over time. – Reed Copsey Dec 09 '11 at 17:27
  • @JohnBubriski Also for the cases where you want to make your objects immutable it's a way to avoid a public setter. – Roman Dec 12 '11 at 04:09
  • @R0MANARMY Sure, but that's a specific piece of functionality. – John B Dec 13 '11 at 13:19
  • @JohnBubriski Yes, it is. You asked why not use object initialization syntax, Reed and I gave you two reasons. Admittedly his is much better than mine. – Roman Dec 13 '11 at 14:26
  • Conversely, not having a default constructor means you can't select instances of EmailData with some LINQ queries. – Tetsujin no Oni Dec 14 '11 at 16:45
  • @TetsujinnoOni How so? This is not an issue with LINQ to Objects, but does require you to be explicit about using the constructor... – Reed Copsey Dec 14 '11 at 19:04
84

If you want to instantiate and add in the same line, you'd have to do something like this:

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

or just instantiate the object prior, and add it directly in:

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

lstemail.Add(data);
slandau
  • 23,528
  • 42
  • 122
  • 184
31

You need to new up an instance of EmailData and then add that:

var data = new EmailData { FirstName = "John", LastName = "Smith", Location = "LA" };

List<EmailData> listemail = new List<EmailData>();
listemail.Add(data);

If you want to able to do:

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

you can create your own custom list, by specializing System.Collections.Generic.List and implementing your own Add method, more or less like this:

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);
    }
}
Christian Horsdal
  • 4,914
  • 23
  • 24
15

One way(in one line) to do it is like this:

listemail.Add(new EmailData {FirstName = "John", LastName = "Smith", Location = "Los Angeles"});
TheBoyan
  • 6,802
  • 3
  • 45
  • 61
13

You need to create an instance of the class to add:

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

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


Alternatively you could declare a constructor for you EmailData object and use that to create the instance.

George Duckett
  • 31,770
  • 9
  • 95
  • 162
8

You're not adding a new instance of the class to the list. Try this:

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

List is a generic class. When you specify a List<EmailData>, the Add method is expecting an object that's of type EmailData. The example above, expressed in more verbose syntax, would be:

EmailData data = new EmailData();
data.FirstName="John";
data.LastName="Smith;
data.Location = "Los Angeles";
lstemail.Add(data);
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
8

You are attempting to call

List<EmailData>.Add(string,string,string)
. Try this:
lstemail.add(new EmailData{ FirstName="John", LastName="Smith", Location="Los Angeles"});
Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
6

How do you expect List<EmailData>.Add to know how to turn three strings 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"));
jason
  • 236,483
  • 35
  • 423
  • 525
6

This line is your problem:

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

There is no direct cast from 3 strings to your custom class. The compiler has no way of figuring out what you're trying to do with this line. You need to Add() an instance of the class to lstemail:

lstemail.Add(new EmailData { FirstName = "JOhn", LastName = "Smith", Location = "Los Angeles" });
David
  • 208,112
  • 36
  • 198
  • 279
6

Here's the extension method version:

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

Usage:

List<EmailData> myList = new List<EmailData>();
myList.Add("Ron", "Klein", "Israel");
Ron Klein
  • 9,178
  • 9
  • 55
  • 88
2
  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;
            }
        }
2

And if you want to create the list with some elements to start with:

var emailList = new List<EmailData>
{
   new EmailData { FirstName = "John", LastName = "Doe", Location = "Moscow" },
   new EmailData {.......}
};
Sunny Milenov
  • 21,990
  • 6
  • 80
  • 106
0
EmailData clsEmailData = new EmailData();
List<EmailData> lstemail = new List<EmailData>(); 

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

lstemail.add(clsEmailData);
Mayank Pandey
  • 185
  • 1
  • 11