-1

I'm trying to make the code will reverse the order of a list from 'Monday, Tuesday ,Wednesday ,Thursday, Friday' to 'Friday, Thursday, Wednesday, Tuesday, Monday'. Whenever I run the code all that appears is

'System.Collections.Generic.List'1[System.String]'

I think the problem may be that I have declared the elements in the list wrong.

class Program
{
    static void Main(string[] args)
    {
        List<string> list = new List<string> (new string[]{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"});
        list.Reverse();
        Console.WriteLine(list);
        Console.ReadLine();
    }
}
Rai Vu
  • 1,595
  • 1
  • 20
  • 30

2 Answers2

0

change

Console.WriteLine(list);

by

foreach(string item in list)
{
    Console.WriteLine(item);
}

You need to iterate the list. Here you have a complete example of the reverse method: https://msdn.microsoft.com/es-es/library/b0axc2h2(v=vs.110).aspx

mnieto
  • 3,744
  • 4
  • 21
  • 37
0

You need to tell the compiler how to convert this list to a string. The default ToString functionality for complex types is to output the name of the type.

What you can do is join the list before outputting it:

Console.WriteLine(string.Join(", ", list));

But it all depends on the exact output you are expecting.

Jerodev
  • 32,252
  • 11
  • 87
  • 108