I was here idle, so I had this curiosity, Someone can tell me what\'s the maximum number of variables per method in C#?
I just tried to compile a generated program source with 26*26*26*26 local variables, not method parameters, (they were called @aaaa
, @aaab
, @aaac
and so on), and I hit this limitation:
error CS0204: Only 65534 locals are allowed
There is no known limit on count of variables, also because any variable can have different size, but there is a memory limit on execution stack size.
Quoting Brian:
The default stack size for a .NET application is 1 MB (default is 256 KB for 32-bit ASP.NET apps and 512 KB for 64-bit ASP.NET apps), but you can change that. For the application you can change the default size by modifying the PE header of the executable. For threads you create, you can use the constructor overload that takes a stack size.
Stack capacity in C#