jf26028
jf26028

Reputation: 43

Why does Func and Action Delegates only take 4 arguments?

Why 4? I know its in the documentation as 4, but that just seems strange.

Upvotes: 2

Views: 988

Answers (6)

Jason Jackson
Jason Jackson

Reputation: 17260

First, I would agree with others that a function signature with 5 args might be getting a bit long. Having said that, I have written plenty of long function signatures in my time as a developer.

You can always write your own overloaded generic delegates. You could even place them in System (I wouldn't).

namespace System
{
  public delegate void Action<T1, T2, T3, T4, T5>(T1 arg1, T2 arg2, 
                                                  T3 arg3, T4 arg4, T5 arg5);
  public delegate ReturnT Action<ReturnT, T1, T2, T3, T4, T5>(T1 arg1, 
                                         T2 arg2, T3 arg3, T4 arg4, T5 arg5);
}

Edit: Since I authored this post .Net 4. has arrived, and with it longer versions of these delegates.

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1063884

In .NET 4.0, you get a lot more - up to 16 parameters.

Although using such routinely might be taking it a bit far in terms of readability...

Upvotes: 1

Rinat Abdullin
Rinat Abdullin

Reputation: 23572

Because it is a bad development pattern to have delegates with that many arguments. It makes your code extremely coupled and complex.

Upvotes: 0

tvanfosson
tvanfosson

Reputation: 532625

I'll go out on a limb and say it's because someone needed an Action or Func delegate somewhere in the framework with 4 arguments and no one has yet needed one with 5. Alternatively, someone decided that any more than 4 and you ought to introduce a class to hold the values instead as @chadmyers says.

Upvotes: 6

Jon Limjap
Jon Limjap

Reputation: 95482

It is strange enough that people would need to consume four types of objects, encapsulate them, manipulate them, and keep track of them all in one class.

I would even posit that anywhere beyond three distinct types and your generic class is already going too far. Usually you have to take into account permutations of how your types interact with each other, e.g., 1 types is fine, 1 x 2 is fine, by 3 you have 6 ways to manipulate your types, by 4 you have 24 combinations to choose from, so you should take this complexity into account as well.

Upvotes: 1

chadmyers
chadmyers

Reputation: 3810

I don't know the exact answer, but I'm guessing it has to do with the fact that if you are passing around delegates with >4 arguments, that's a code smell and you're likely doing something wrong. You should consider using the "Introduce Parameter Object" refactoring.

Upvotes: 2

Related Questions