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?
-
11This would be a good question for @JonSkeet :) – Tim Sep 30 '12 at 05:30
-
12Because knowing the answers to seemingly trivial questions like this can save you hours or days of painful debugging hell...especially if an auto-generated method generates methods with extremely large parameter counts (called arity in Computer Science lingo). – rmiesen Jun 16 '13 at 08:17
2 Answers
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):
- A .net application containing a method with 16383 parameters can be compiled, ran, and called (!)
- A .net application containing 16384 or more parameters can be compiled, but running such a program throws an unstated exception.
- A .net application containing 50000 parameters can also be compiled, but attempting to run such an application results in a StackOverflowException being thrown.
- Attempting to compile a .net application containing 100000 parameters or more results in csc.exe giving a compile-time error, stating that the resulting expression is too long or complex to handle.
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

- 2,470
- 1
- 21
- 15
-
18those numbers depend on the architecture of the operating system and the memory of the computer. – Ionut Hulub Sep 30 '12 at 05:31
-
3I feel memory will be affected more by the number of value parameters being passed into the functions as well as the type of the parameter, suppose you sent 16383 strings into the function with each string being about 2MB in size, will the program still run without exception on the test machine??. I saw the code uploaded by @rmiesen and it was full of int parameters. – Surya Pratap Oct 01 '12 at 10:35
-
It's possible that the maximum number of parameters you can pass to a method is limited by system memory, but it can't be the only limit. A System.Int32 is 4 bytes in size, the memory used by all the passed arguments is 64k. As a side note, I did some math and 16383 is one less that 2^14. I wonder if that is significant in any way... – rmiesen Oct 01 '12 at 17:50
-
50-16383 has 16384 (2^14) total states, or exactly 14 bits worth. As the extended joke goes, this is one of the two hard problems in computer science: Cache invalidation, naming things, and off-by-one errors. :) – Charles Burns Mar 21 '16 at 15:54
-
Does anyone know what are the maximum number of parameters that a delegate can accept ? – Jamshaid K. Nov 14 '16 at 16:51
-
Inline functions (and probably delegates, which they're shorthand for) can accept up to 16 inputs. – Extragorey Feb 01 '18 at 04:10
-
FYI, throws `System.InvalidProgramException: JIT Compiler encountered an internal limitation.` exception ♂️ – guneysus Dec 18 '19 at 10:36
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
.

- 3,156
- 22
- 32
-
4CIL also stores lengths of parameter arrays in unsigned shorts, so the length can't be more than 65,535. – Jon Hanna Nov 16 '17 at 18:31