Albi
Albi

Reputation: 33

List not printing out information when called in main

I have a List called

private List<Car> Cars; //Car is another class

I want to create a new list from the information in the List< Car > Cars that uses parameters to specify a year range to extracted from the Cars list and then placed into a new list called

private List<Car> getCars;

The code is as follows. Please note it is only part of a project so not all code is provided.

    private List<Car> Cars;
    private List<Car> getCars;

    public List<Car> GetCars(int fromYear, int toYear) 
    {
        getCars = new List<Car> { };
            foreach (Car c in Cars)
            if (c.Year >= fromYear && c.Year <= toYear)
                getCars.Add(c);
        return getCars;
    }

The problem I'm having is although there are no errors showing up when I run the code the new list does not print out, instead it print's out
System.Collection.Generic.List'1[Lab__2.Car]

Any help would be great in how to make it print out the list's objects instead of what is above. Finally My lecturer has specified that he wants the method formatted as such

public List<Car> GetPrices(int year)
{
}

Upvotes: 0

Views: 576

Answers (5)

RePierre
RePierre

Reputation: 9566

As @Ciara specified, you must print each item yourself. When you issue something like Console.WriteLine(car) in your code, the Console class automatically calls ToString() method on your Car object.

The documentation for ToString() method on MSDN specifies:

The default implementation of the ToString method returns the fully qualified name of the type of the Object [...]

In your case, that name is System.Collection.Generic.List'1[Lab__2.Car]. To change what ToString() method returns, in your Car class override the method:

public class Car
{
    public string Make { get; set; }
    public int Year { get; set; }

    public override string ToString()
    {
        return String.Format("Make: {0}, Year: {1}", Make, Year);
    }
}

Afterwards you need to iterate the list and print each item:

carList.ForEach(Console.WriteLine);

Upvotes: 0

Peter - Reinstate Monica
Peter - Reinstate Monica

Reputation: 16026

Try something along these lines:

public Class Car
{   // guessing here
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    public override string TosString()
    {
        return "Make: " + Make + ", Model: " + Model + ", Year: " + year;
    }

And then, somewhere in your program:

foreach(var car in CarList.Where(c => c.Year >= fromYear && c.Year <= toYear))
{ 
    Console.Out.WriteLine(car);
}

Note how the functionality of your GetCars() can be expressed in a fairly readable Linq Where method call applied to the list.

Upvotes: 0

Raimund Kr&#228;mer
Raimund Kr&#228;mer

Reputation: 1309

You can print the items of the list by iterating over them, and printing each object individually:

for (Car car in Cars)
{
    Console.WriteLine(car.ToString);
}

Doing this Console.WriteLine(Cars) will only give you information about the List object, usually being the full type and maybe the address in memory, depending on the runtime.

If you want to print the items of the list after getting them from a method, do this:

for (Car car in GetCars(fromYear, toYear))
{
    Console.WriteLine(car.ToString);
}

When printing an object that is not a string (or other character sequence) you might want to override the ToString method that is inherited from Object in order to specify the information of the object (or Car in this case) you want to print.

Upvotes: 0

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

Reputation: 6251

You are surely calling the ToString() method directly on the List but it will only print its type:

Default implementations of the Object.ToString method return the fully qualified name of the object's type. - MSDN

So, you must iterate through the items in the list and print it's details. Example:

foreach (Car c in Cars) {
     Console.WriteLine(c.Name); //I do not know what properties you have in the class Car. Change accordingly.
}

Or you can use String.Join():

String.Join(Environment.NewLine, Cars); //You can change 'Environment.NewLine' to ", " if you want a comma instead of a new line.

Upvotes: 0

Ciara
Ciara

Reputation: 394

What you're seeing is the output you get when you call print directly on a list. It won't automatically print the contents, you must print each item yourself.

Upvotes: 2

Related Questions