我遇到了一段代码,仅当for循环重复一定次数时,该代码才会导致堆栈溢出异常。
这是代码:
public class Stackoverflow
{
public static void Test()
{
List<int> list = new List<int>() {1, 2};
Container container = new Container {List = list};
for (int i = 0; i < 10000; i++) // This matters
{
foreach (var item in container.List)
{
Console.WriteLine(item);
}
}
}
}
public class Container
{
private IEnumerable<int> list;
public IEnumerable<int> List
{
get
{
//return list.OrderBy(x => x); <- This is OK
list = list.OrderBy(x => x); // This is not …Run Code Online (Sandbox Code Playgroud)