I am trying to figure out what the maximum number of parameters a method in C# can have. I've checked everywhere for an answer, including the C# official documentation, MSDN, and a couple of CLR references and I can't find an answer. Does anyone have an answer to this question?
Thanks! as I know (and your first link says) the limit is 256 arguments in c++ and 127 in c. The line length is no problem since you can break a line into more lines.
The maximum number of parameters is limited to 126 by functions.
they say that - 255 parameters...
— Parameters in one macro definition [256].
I used a throwaway program to create a program to determine the maximum number of parameters I can pass to a method. Based on the results of my experimentation, the closest to an answer I can find are the following (all of which is only valid on my computer):
Aside from that, does anyone have a definitive answer to this question?
P.S. If anyone wants to try this experiment on their computer, you can start with my test program, which can be downloaded https://docs.google.com/open?id=0B1Q3uRTkbsXic2cwUFpXanNkSk0
Here is your theoretical answer:
In order to push method arguments onto the stack, compiled code has the following MSIL opcodes to choose from:
ldarg.0
ldarg.1
ldarg.2
ldarg.3
ldarg.S
ldarg
ldarg.0
to ldarg.3
is used to push the first 4 method arguments onto the stack (including this
as the first argument for instance methods).
ldarg.S
takes an 8-bit argument number, and so it can be used to push up to 256 arguments onto the stack.
That leaves us with plain old ldarg
, which can handle the most method arguments: it takes an unsigned 16-bit argument number, so the largest number of arguments that can be successfully compiled into valid MSIL is 2^16 = 65,536
.
As others have noted, however, the actual limit will depend on implementation details of the runtime. Based on rmiesen's answer, it looks like the current .NET implementation limits the maximum number of parameters to 2^14
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With