palm snow
palm snow

Reputation: 2392

Why I am getting this compilation error?

Why I cannot use option #1 below. Option 2 works fine

class Program
    {
        static void Main()
        {
             //Option 1  
            //Error 1   The best overloaded method match for 'ConsoleApplication2.Program.SomeMethod(System.Collections.Generic.List<string>)' has some invalid argument
            //Error 2   Argument 1: cannot convert from 'void' to 'System.Collections.Generic.List<string>'
            SomeMethod(new List<string>().Add("This give compilation Error"));

            //Option 2 
            List<string> MyMessages = new List<string>();
            MyMessages.Add("This compiles fine");
            SomeMethod(MyMessages);
        }

        static void SomeMethod(List<string> Messages)
        {
            foreach (string Message in Messages)
                Console.WriteLine(Message);
        }
    } 

Upvotes: 3

Views: 300

Answers (12)

Dewasish Mitruka
Dewasish Mitruka

Reputation: 2896

Try this :

class Program
        {
            static void Main()
            {

                //There was a syntax error in your code. It should be declare like this
                SomeMethod(new List<string>(){("This give compilation Error")});

                //Option 2 
                List<string> MyMessages = new List<string>();
                MyMessages.Add("This compiles fine");
                SomeMethod(MyMessages);
            }

            static void SomeMethod(List<string> Messages)
            {
                foreach (string Message in Messages)
                    Console.WriteLine(Message);
            }
        }

Upvotes: 4

msarchet
msarchet

Reputation: 15242

SomeMethod(new List<string>() {"This give compilation Error"});

Upvotes: 1

Matek
Matek

Reputation: 61

You see this error because Add method doesn't return anything. You can change this line to:

SomeMethod(new List<string>(){"This won't give compilation Error"});

Upvotes: 2

koneru
koneru

Reputation: 76

Because Add method return type is Void and your expecting it to return collection.Check the documentation http://msdn.microsoft.com/en-us/library/3wcytfd1.aspx public void Add( T item )

Upvotes: 1

Minustar
Minustar

Reputation: 1225

AFAIK, the Add() method of generic List doesn't return an int, it's void.

Upvotes: 0

Nat
Nat

Reputation: 3727

Because .Add() return void type instead of List. You can however do this

SomeMethod(new List<string>() { "This give compilation Error" });

Upvotes: 5

Pieter
Pieter

Reputation: 3399

List.Add returns void and that's what you're passing into SomeMethod. Obviously that's not going to work.

Upvotes: 1

user596075
user596075

Reputation:

Because in your first option, you are passing the return of the Add() method to SomeMethod(), not the actual List<string> object.

Upvotes: 0

Erix
Erix

Reputation: 7105

List<T>.Add(T someItem) does not return a reference to the list as a result of the operation, it returns void

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44605

new List<string>().Add("This give compilation Error")

returns void but the method SomeMethod expects a List<string>

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039378

It's because the List<T>.Add() method doesn't return the element that you just added to the list. It returns void.

But you could do this:

SomeMethod(new List<string>(new[] { "This compiles fine" }));

or use the collection initializer syntax:

SomeMethod(new List<string> { "This compiles fine" });

and if you wanted multiple elements:

SomeMethod(new List<string> { "elem1", "elem2", "elem3" });

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1503140

List<T>.Add returns void. Your code fails in the same way that this would fail:

List<string> list = new List<string>().Add("This wouldn't work");

However, C# 3 to the rescue with collection initializers:

SomeMethod(new List<string> { "Woot!" });

Upvotes: 14

Related Questions