I have a simple example of some code that causes a stack overflow exception and wondered if this was a common issue or some kind of bug.
I am using a console app to generate some data. It adds around 20000 objects to a collection. Some of the fields are nullable. If I make them bool? then it works but if I change a couple of them (as I have in the sample code) to decimal? then it throws the exception.
It also only does this when I physically add 20000 Add(... lines. If I do it in a loop then it work fine (this is in the example as well).
Apologies for the length of the code example. Any help would be appreciated.
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
class. And if you meantnew CreateCommand
that doesn't have a parameterless constructor nor does it define aCount
method. – juharr Dec 5 at 16:13Main
method runs fine. The second does not compile, so you need to give use more info in order to figure out what is going on. – juharr Dec 5 at 16:16Count
in that it is inherited from the base class. – Servy Dec 5 at 16:16new CreateCommands()
. – juharr Dec 5 at 16:17