C# 如何在foreach中反转列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15086419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:00:03  来源:igfitidea点击:

How can I reverse a list in foreach?

c#list

提问by gurehbgui

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

我有一个问题,我无法逆转以下内容List

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

I always get the error:

我总是收到错误:

Type void is not enumerable

类型 void 不可枚举

Whats the problem and how to solve it?

问题是什么以及如何解决?

采纳答案by Jon Skeet

List<T>.Reversedoesn't returnanything - it reverses the list in place.

List<T>.Reverse返回任何内容 - 它反转列表到位。

If you want to use the LINQ version of Reversewhich returns a reversed sequence but withoutmutating the existing list, you could use:

如果要使用其返回反向序列但改变现有列表的LINQ 版本,Reverse则可以使用:

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))
    {
        ...
    }
}

回答by MarcinJuraszek

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

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

Reverse更改列表中的顺序 - 它不会返回项目顺序相反的新列表。

回答by Daniel Hilgarth

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

List<T>.Reverse()确实的就地反向。这意味着它会更改您的原始列表。

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.Reverseexplicitly:

如果您不想更改原始列表,则必须Enumerable.Reverse明确调用:

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

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

不能Enumerable.Reverse在扩展方法语法中使用的原因是:扩展方法不会隐藏/覆盖实例方法,并且List<T>恰好已经有一个Reverse方法。

回答by Parimal Raj

List<T>.Reversedo not return anything all it!

List<T>.Reverse什么都不要归还啦!

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

    }

}

回答by Soner G?nül

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

List<T>.Reverse()是一个就地反向,它不会返回一个新的 list。它会更改您的原始列表。

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

颠倒整个 中元素的顺序List<T>

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

您需要row.Reverse();在第一个 foreach 语句中使用。喜欢;

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.Reversemethod instead of.

如果您不想更改原始列表,则可以使用Enumerable.Reverse方法代替。

Inverts the order of the elements in a sequence.

反转序列中元素的顺序。

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

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

这是与使用方法相同的演示Enumerable.Reverse<T>

回答by ryadavilli

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

List.Reverse() 是一个带有 void 签名的方法。

You can probably change your loop as below.

你可能可以改变你的循环如下。

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