我有一个代码导致堆栈溢出异常,并想知道这是一个常见问题或某种错误的简单示例。堆栈溢出错误可空数字类型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