0

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)
{
}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Albi
  • 33
  • 5
  • What print do you expect? – Jens Nov 03 '15 at 12:42
  • 2
    How are you calling `GetCars()`? It returns a `List<>` object. My guess is you are calling `.ToString()` on the collection itself. – gmiley Nov 03 '15 at 12:43
  • 1
    You may find this useful: [How to display list items on console window in C#](http://stackoverflow.com/questions/759133/how-to-display-list-items-on-console-window-in-c-sharp) – awesoon Nov 03 '15 at 12:43
  • 1
    You don't show the code which actually tries to print. Something like `foreach(var c in carlist) { Console.Out.WriteLine("Make: " + c.Make + ", Model: " + c.Model + ", Year: " + c.year + Environment.Newline);`? Edit: Or better the advice in @soon 's link; define a ToString() method for a car and then just print each car as an entity. Edit2: Define an extension method for List which does that for any T. – Peter - Reinstate Monica Nov 03 '15 at 12:44
  • 1
    You might want to print the names of the cars by iterating through the list. calling tostring on the list will print its type. – Fᴀʀʜᴀɴ Aɴᴀᴍ Nov 03 '15 at 12:45

5 Answers5

2

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.

Ciara
  • 384
  • 2
  • 12
0

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.
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
  • To ensure encapsulation, rather override the `ToString()` method of the `Car` class, instead of directly accessing puplic members. – Raimund Krämer Nov 03 '15 at 13:08
0

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.

Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62
0

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);
RePierre
  • 9,358
  • 2
  • 20
  • 37
0

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.

Raimund Krämer
  • 1,255
  • 1
  • 11
  • 29