user3486393
user3486393

Reputation: 13

Calling a method that includes a List of an object as Parameter

I have a method that looks like following:

method(List<Car> list)
{

}

How do i call a method in main that has a list of Car as parameter?

Upvotes: 0

Views: 82

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1062570

by.... creating a list of cars, adding the cars you are interested in, and calling it?

var list = new List<Car>();
list.Add(new Car { Color = "Blue" });
list.Add(new Car { Color = "Green" });
method(list);

The bigger issue, perhaps, is "how do I call a non-static method from a static method such as Main()", to which there are two possible answers:

  • make the method static (if it doesn't need instance state)
  • create an instance of the declaring type

Assuming we mean the latter:

var obj = new SomeType(); // assuming an accessible parameterless ctor exists
obj.method(list);

Upvotes: 2

Bill
Bill

Reputation: 1479

Is this not working?

var cars = new List<Car>(); //Get your list of cars
method(cars);

Upvotes: 0

Related Questions