gurehbgui
gurehbgui

Reputation: 14684

How can I reverse a list in foreach?

I have a problem, I cant reverse the following List:

foreach (List<Foo> row in Items) 
{
    foreach (Foo item in row.Reverse())
    {
        ...
    }
}

I always get the error:

Type void is not enumerable

Whats the problem and how to solve it?

Upvotes: 14

Views: 35450

Answers (6)

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98750

List<T>.Reverse() is an in-place reverse, it doesn't return a new list. It changes your orininal list.

Reverses the order of the elements in the entire List<T>.

You need to use row.Reverse(); in your first foreach statement. Like;

foreach (List<Foo> row in Items) 
{
    row.Reverse();
    foreach (Foo item in row)
    {
        //
    }
}

Here is a DEMO.

If you don't want to change your orininal list, you can use Enumerable.Reverse method instead of.

Inverts the order of the elements in a sequence.

foreach (Foo item in Enumerable.Reverse(row))
{
    //
}

Here is the same DEMO with using Enumerable.Reverse<T> method.

Upvotes: 5

Ravi Y
Ravi Y

Reputation: 4376

List.Reverse() is a method with a void signature.

You can probably change your loop as below.

foreach (List<Foo> row in Items) 
{
    row.Reverse();
    foreach (Foo item in row)
    {
        ...
    }
}

Upvotes: 3

Parimal Raj
Parimal Raj

Reputation: 20575

List<T>.Reverse do not return anything all it!

foreach (IEnumerable<Foo> row in Items) 
{
    row.Reverse();
    foreach(Foo item in row)
    {

    }

}

Upvotes: 3

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

foreach (List<Foo> row in Items) 
{
    row.Reverse()
    foreach (Foo item in row)
    {
        ...
    }
}

Reverse change order within the list - it does not return new list with reversed order of items.

Upvotes: 4

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

List<T>.Reverse() does an in-place reverse. That means it changes your original list.

So, you would use it like this:

foreach (List<Foo> row in Items) 
{
    row.Reverse();
    foreach (Foo item in row)
    {
        ...
    }
}

If you don't want to change your original list, you will have to call Enumerable.Reverse explicitly:

foreach (List<Foo> row in Items) 
{
    foreach (Foo item in Enumerable.Reverse(row))
    {
        ...
    }
}

The reason for not being able to use Enumerable.Reverse in the extension method syntax is: Extension methods don't hide / override instance methods and List<T> happens to already have a Reverse method.

Upvotes: 20

Jon Skeet
Jon Skeet

Reputation: 1500185

List<T>.Reverse doesn't return anything - it reverses the list in place.

If you want to use the LINQ version of Reverse which returns a reversed sequence but without mutating the existing list, you could use:

foreach (IEnumerable<Foo> row in Items) 
{
    foreach (Foo item in row.Reverse())
    {
        ...
    }
}

Or perhaps more clearly:

foreach (List<Foo> row in Items) 
{
    // We want to use the LINQ to Objects non-invasive
    // Reverse method, not List<T>.Reverse
    foreach (Foo item in Enumerable.Reverse(row))
    {
        ...
    }
}

Upvotes: 38

Related Questions