LINQはスタックをLIFOの順序で繰り返します[重複]
Dec 08 2020
LINQメソッドはスタック(および私の特定のケースではStack
簡単な質問です。「はい」と「はい」を想定しますが、非常に単純なため、検索によって大量のLINQおよびStack関連の回答が生成され、閲覧した無数の結果のいずれもこのケースに具体的に回答しませんでした。
回答
1 AndrewH Dec 08 2020 at 17:24
はい。
次の実験は、スタックのLIFOの順序に従っていることを示しています。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class DoesLinqIterateAStackInLifo
{
public static void Main()
{
var stack = new Stack<char>();
stack.Push('s');
stack.Push('e');
stack.Push('Y');
Console.WriteLine(stack.Aggregate(new StringBuilder(), (builder,letter) => builder.Append(letter)));
Console.WriteLine(string.Join("",stack.ToList()));
if(stack.Count==3)
{
Console.WriteLine("And it will not pop the stack");
}
}
}
出力:
Yes
Yes
And it will not pop the stack