This is caused by the fact that, effectively, your inline list initialisation is too large for the stack - see this very related question over on the msdn forums where the scenario is nigh-on identical.
A method in .Net has both a stack depth and size. A StackOverflowException is caused not just by the number of calls on the stack, but the overall size of each method's allocation of memory in the stack. In this case, your method is just far too large - and that's caused by the number of local variables.
By way of example, consider the following code:
public class Foo
{
public int Bar { get; set;}
}
public Foo[] GetInts()
{
return new Foo[] { new Foo() { Bar = 1 }, new Foo() { Bar = 2 },
new Foo() { Bar = 3 }, new Foo() { Bar = 4 }, new Foo() { Bar = 5 } };
}
Now look at the lead-in IL of that method when compiled (this is a release build, too):
.maxstack 4
.locals init (
[0] class SomeExample/Foo '<>g__initLocal0',
[1] class SomeExample/Foo '<>g__initLocal1',
[2] class SomeExample/Foo '<>g__initLocal2',
[3] class SomeExample/Foo '<>g__initLocal3',
[4] class SomeExample/Foo '<>g__initLocal4',
[5] class SomeExample/Foo[] CS$0$0000
)
Note - the actual bit before the /, i.e. SomeExample will depend on the namespace and class in which the method and nested class are defined - I've had to edit a few times to remove the typenames from some in progress code that I'm writing at work!
Why all those locals? Because of the way that inline initialisation is performed. Each object is newed and stored in a 'hidden' local, this is required so that the property assignments can be performed on the inline initialisations of each Foo (the object instance is required to generate the property set for Bar). This also demonstrates how inline initialisation is just some C# syntactic sugar.
In your case, it's these locals that's causing the stack to blow (there are at least a few thousand of them just for the top-level objects - but you also have nested initialisers as well).
The C# compiler could alternatively pre-load the number of references required for each on to the stack (popping each one off for each property assignment) but then that is abusing the stack where the use of locals will perform much better.
It could also use a single local, since each is merely written-too and then stored in the list by array index, the local is never needed again. That might be one for the C# team to consider - Eric Lippert may have a few thoughts about this if he stumbles on this thread.
Now, this examination also gives us a potential route around this use of locals for your very massive method: use an iterator:
public Foo[] GetInts()
{
return GetIntsHelper().ToArray();
}
private IEnumerable<Foo> GetIntsHelper()
{
yield return new Foo() { Bar = 1 };
yield return new Foo() { Bar = 2 };
yield return new Foo() { Bar = 3 };
yield return new Foo() { Bar = 4 };
yield return new Foo() { Bar = 5 };
}
Now the IL for GetInts() now simply has .maxstack 8 at the head, and no locals. Looking at the iterator function GetIntsHelper() we have:
.maxstack 2
.locals init (
[0] class SomeExample/'<GetIntsHelper>d__5'
)
So now we've stopped using all those locals in those methods...
But...
Look at the class SomeExample/'<GetIntsHelper>d__5', which has been automatically generated by the compiler - and we see that the locals are still there - they've just been promoted to fields on that class:
.field public class SomeExample/Foo '<>g__initLocal0'
.field public class SomeExample/Foo '<>g__initLocal1'
.field public class SomeExample/Foo '<>g__initLocal2'
.field public class SomeExample/Foo '<>g__initLocal3'
.field public class SomeExample/Foo '<>g__initLocal4'
So the question is - will the creation of that object also blow the stack if applied to your scenario? Probably not, because in memory it should be like trying to initialise large array - where million-element arrays are quite acceptable (assuming enough memory in practise).
So - you might be able to fix your code quite simply by moving over to using an IEnumerable method that yields each element.
But best practise says that if you absolutely have to have this statically defined - consider adding the data to an embedded resource or file on disk (XML and Linq to XML might be a good choice) and then loading it from there on demand.
Better still - stick it in a database :)
var sellers = DataHelper.Sellersbut that's not what generates the SO exception. You mentioned something about ASP.NET MVC site. Could you tell us a little more about it? Are you using this in a controller action or something? Are you returning this as JSON? Are you serializing or something? Context please!