views:

101

answers:

3

I am working with System.Func but have reached a stumbling block with it.

System.Func<TReturn> // (no arg, with return value)
System.Func<T, TReturn> // (1 arg, with return value)
System.Func<T1, T2, TReturn> // (2 arg, with return value)
System.Func<T1, T2, T3, TReturn> // (3 arg, with return value)
System.Func<T1, T2, T3, T4, TReturn> // (4 arg, with return value)

The max it accepts is 4 arguments.

Is there any way of extending this to 5 arguments?

+7  A: 

Move to a higher version of .NET framework. E.g. .NET 4.0 has up to 16

http://msdn.microsoft.com/en-us/library/yxcx7skw.aspx

whereas 3.5 has just 4

http://msdn.microsoft.com/en-us/library/yxcx7skw(v=VS.90).aspx

Or, if you stuck in a lower version, just define the delegate yourself.

Brian
Thanks for your help Brian.I am stuck in .net 3.5 and cant upgrade.Could you assist me in defining a delegate myself as I am not too familiar with doing such? your help would be appreciated.
Niall Collins
The syntax is in the docs: http://msdn.microsoft.com/en-us/library/bb534303(v=VS.90).aspx
Brian
@Niall: The syntax you need would be `public delegate TResult Func<T1, T2, T3, T4, T5, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);` etc.
LukeH
I'd hate to see code that actually uses a delegate with 16 parameters ;)
Thomas Levesque
+1  A: 

I guess, it is possible to see Action and Func at least with 25 parameters in framework 5.0 :)

There is not any reason to move to the higher framework version because i cannot refactore my code to valid, maintainable view. Really it is not so good practice to use so many parameters in the method's of function's signature. Use entity that has 5 fields or properties. You can expend this entity without changing method or function signature.

igor
+3  A: 

You have a few options one is to define the delegate your self which would look like:

public delegate TResult Func<T1,T2,...,TN,TResult>(T1 arg1, T2 arg2,...,TN argN); 

you can basically define it for any number of arguments (higher than 4 since you might get a name clash otherwise)

or you can wrap your arguments into a structure of some sort so that you can use one of the Func delegates already defined for you.

In any case you should worry about the method signature if you cannot use one of the predefined Func delegates. Quite often long lists of parameters are a smell that often leads you to realize that the method is doing to much (unrelated) work.

My personal approach would thus be to figure out where the design failed and correct that rather than correct what is most often the symptom (in this case defining a Func with sufficient agruments could be fixing the symptom not saying that it is since I don't know your code)

Rune FS