Tom Gullen
Tom Gullen

Reputation: 61737

c# Trying to reverse a list

I have the following code:

public class CategoryNavItem
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string Icon { get; set; }

    public CategoryNavItem(int CatID, string CatName, string CatIcon)
    {
        ID = CatID;
        Name = CatName;
        Icon = CatIcon;
    }
}

public static List<Lite.CategoryNavItem> getMenuNav(int CatID)
{
    List<Lite.CategoryNavItem> NavItems = new List<Lite.CategoryNavItem>();

    -- Snipped code --

    return NavItems.Reverse();
}

But I get the following error:

Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<Lite.CategoryNavItem>'

Any ideas why this might be?

Upvotes: 110

Views: 128371

Answers (9)

Igor Fujs
Igor Fujs

Reputation: 9

The over-complicated code people are writing these days is overwhelming. Using enumerators and LINQ and so on... when a simple method can do this:

public void Reverse<T>(List<T> list)
{
  int last = list.Count - 1;

  int middle = list.Count >> 1;

  for (int i = 0; i < middle; i++) (list[i], list[last - i]) = (list[last - i], list[i]);
}

Upvotes: -1

Bodix
Bodix

Reputation: 494

I had a situation where none of the suggested options suited me. So, if you:

  • don't want to use someList.Reverse() because it returns nothing (void)
  • don't want to use someList.Reverse() because it modifies source list
  • use someList.AsEnumerable().Reverse() and get the Ambiguous invocation error

You can try Enumerable.Reverse(someList) instead.

Don't forget the:

using System.Linq;

Upvotes: 6

MLH
MLH

Reputation: 159

If you have a list like in your example:

List<Lite.CategoryNavItem> NavItems

You can use the generic Reverse<> extensions method to return a new list without modifiying the original one. Just use the extension method like this:

List<Lite.CategoryNavItem> reversed = NavItems.Reverse<Lite.CategoryNavItem>();

Notes: You need to specify the <> generic tags to explicit use the extension method. Don't forget the

using System.Linq;

Upvotes: 4

Mafu Josh
Mafu Josh

Reputation: 2672

One workaround would be Return NavItems.AsEnumerable().Reverse();

Upvotes: 133

JK.
JK.

Reputation: 5136

Reverse() does not return a List as expected of your function.

NavItems.Reverse();
return NavItems;

Upvotes: 6

Yahia
Yahia

Reputation: 70369

.Reverse reverses the "in-place"..., try

NavItems.Reverse();
return NavItems;

Upvotes: 3

sll
sll

Reputation: 62494

Reverse() does not returns reversed list itself, it modifies original list. So rewrite it as following:

return NavItems.Reverse(); 

TO

NavItems.Reverse(); 
return NavItems;

Upvotes: 11

Marc Gravell
Marc Gravell

Reputation: 1062650

Try:

NavItems.Reverse();
return NavItems;

List<T>.Reverse() is an in-place reverse; it doesn't return a new list.

This does contrast to LINQ, where Reverse() returns the reversed sequence, but when there is a suitable non-extension method it is always selected in preference to an extension method. Plus, in the LINQ case it would have to be:

return someSequence.Reverse().ToList();

Upvotes: 168

Kieren Johnstone
Kieren Johnstone

Reputation: 41993

.Reverse() on a list reverses the items within the list, it does not return a new reversed list.

Upvotes: 22

Related Questions