c# Tratando de revertir una lista


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();
}

El reverso no funciona:

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

¿Alguna idea de por qué podría ser esto?

Author: Tom Gullen, 2011-08-08

6 answers

Intenta:

NavItems.Reverse();
return NavItems;

List<T>.Reverse() es un in-place inverso; no devuelve una nueva lista.

Esto hace contraste con LINQ, donde Reverse() devuelve la secuencia invertida, pero cuando hay un método adecuado sin extensión, se selecciona siempre en lugar de un método de extensión. Además, en el caso LINQ tendría que ser:

return someSequence.Reverse().ToList();
 107
Author: Marc Gravell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-08 10:13:07

Una solución sería Return NavItems.AsEnumerable().Reverse();

 71
Author: Mafu Josh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-10-07 17:20:46

.Reverse() en una lista invierte los elementos dentro de la lista, no devuelve una nueva lista invertida.

 16
Author: Kieren Johnstone,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-08 10:13:01

Reverse() no devuelve la lista invertida en sí, modifica la lista original. Así que reescríbelo de la siguiente manera:

return NavItems.Reverse(); 

A

NavItems.Reverse(); 
return NavItems;
 8
Author: sll,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-08 10:14:24

Reverse() no devuelve una Lista como se espera de su función.

NavItems.Reverse();
return NavItems;
 5
Author: JK.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-08 10:18:50

.Reverse invierte el "in-place"..., try

NavItems.Reverse();
return NavItems;
 3
Author: Yahia,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-08 10:15:15