Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum number of variables in method

Tags:

c#

I was here idle, so I had this curiosity, Someone can tell me what's the maximum number of variables per method in C#?

like image 783
Only a Curious Mind Avatar asked Mar 19 '14 11:03

Only a Curious Mind


2 Answers

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

like image 130
Jeppe Stig Nielsen Avatar answered Sep 22 '22 08:09

Jeppe Stig Nielsen


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#

like image 45
Tigran Avatar answered Sep 25 '22 08:09

Tigran