我有一個代碼導致堆棧溢出異常,並想知道這是一個常見問題或某種錯誤的簡單示例。堆棧溢出錯誤可空數字類型c#
我正在使用控制檯應用程序來生成一些數據。它將大約20000個對象添加到集合中。一些字段是可空的。如果我讓他們布爾?那麼它的工作原理,但如果我將其中的幾個(如我在示例代碼中)更改爲十進制?那麼它會拋出異常。
它也只做到這一點,當我身體增加20000添加(...行。如果我在一個循環做到這一點,然後它工作正常(這是在本例中爲好)。
道歉長度的代碼的例子。任何幫助,將不勝感激。
using System.Collections.Generic;
using System;
namespace StackOverflow
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"{new UpdateCommands().Count} commands");
Console.WriteLine($"{new CreateCommands().Count} commands");
Console.ReadKey();
}
}
}
public class CreateCommand
{
public CreateCommand(string code, string name, string label, string field1, string field2, string field3,
bool? field4, bool? field5, bool? field6, bool? field7, decimal? field8, decimal? field9, decimal? field10, decimal? field11)
{
}
}
public class UpdateCommands : List<CreateCommand>
{
public UpdateCommands()
{
for (int i = 0; i < 22000; i++)
{
Add(new CreateCommand("code", "name", "label", "", null, null, null, null, null, null, null, null, null, null));
}
}
}
public class CreateCommands : List<CreateCommand>
{
public CreateCommands()
{
Add(new CreateCommand("code", "name", "label", "", null, null, null, null, null, null, null, null, null, null));
you need to copy the line above 22000 times
}
}
這不會編譯,因爲沒有'CreateCommands'類。如果你的意思是'新的CreateCommand',它沒有無參數構造函數,也沒有定義'Count'方法。 – juharr
我認爲問題的目的是爲什麼你有很多行時得到stackoverflow。這不是一個愚蠢的問題。因爲OP已經知道如何使用循環,這是爲了學習的目的。 –
我剛剛測試過這個,'Main'方法的第一行運行良好。第二個不能編譯,所以你需要使用更多的信息來弄清楚發生了什麼。 – juharr