C#试图扭转名单 [英] c# Trying to reverse a list

查看:103
本文介绍了C#试图扭转名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



反向不起作用:

The reverse doesn't work:

错误3无法隐式转换类型'无效'到'System.Collections.Generic.List< Lite.CategoryNavItem>

任何想法,为什么这可能是

Any ideas why this might be?

推荐答案

尝试:

NavItems.Reverse();
return NavItems;



列表< T> .Reverse()是的原位的反转;它不返回一个新的列表。

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

这的确实的对比,LINQ,其中反向()返回的相反的顺序,但是当有一个合适的非扩展方法是始终优先选择扩展方法。另外,在LINQ情况下,它必须是:

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

这篇关于C#试图扭转名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆