public class CategoryNavItem
{
public int ID { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public CategoryNavItem(
.Reverse()
on a list reverses the items within the list, it does not return a new reversed list.
One workaround would be Return NavItems.AsEnumerable().Reverse();
Reverse()
does not return a List as expected of your function.
NavItems.Reverse();
return NavItems;
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();
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;
Reverse()
does not returns reversed list itself, it modifies original list. So rewrite it as following:
return NavItems.Reverse();
TO
NavItems.Reverse();
return NavItems;