views:

344

answers:

4

Why would this compile:

public Dictionary<ValueLineType, 
                  Func<HtmlHelper, 
                       string, 
                       object, 
                       Type, 
                       string>> constructor = 
       new Dictionary<ValueLineType, 
                      Func<HtmlHelper, 
                           string, 
                           object, 
                           Type, 
                           string>>();

but not this other one with one extra parameter in the Func (the boolean):

public Dictionary<ValueLineType, 
                  Func<HtmlHelper, 
                       string, 
                       object, 
                       Type, 
                       bool,  
                       string>> constructor = 
       new Dictionary<ValueLineType, 
                      Func<HtmlHelper, 
                           string, 
                           object, 
                           Type, 
                           bool, 
                           string>>();

Either I'm getting blind or there's something else I'm going to learn today :D

+3  A: 

There are different classes defined by the framework named Func that take from 1 to 5 parameters. You'd need to define your own class that takes 6.

RossFabricant
Func only defines as far as 4 parameters - but 5 *type* parameters.
Jon Skeet
+16  A: 

There is no such thing as Func<T1,T2,T3,T4,T5,TResult>. It only goes as far as 4 parameters (i.e. 5 type parameters, including one for the return value):

Func<T>
Func<T1, TResult>
Func<T1, T2, TResult>
Func<T1, T2, T3, TResult>
Func<T1, T2, T3, T4, TResult>
SpinalTap<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>

You can declare your own, of course:

public delegate TResult Func<T1, T2, T3, T4, T5, TResult>
    (T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);

However, at that point I'd think really carefully about whether you might be able to encapsulate some of those parameters together. Are they completely unrelated?

Jon Skeet
But there is no reason you couldn't add your own since they are just delegates with generics.
Samuel
Indeed. Editing...
Jon Skeet
@Me: Add your own with more than 4 parameters.
Samuel
+1 Nice answer, yours covers mine as well so I will delete.
Andrew Hare
+1 for the Nigel Tufnel reference
tvanfosson
I love the Spinal Tap reference :) + 1
DoctaJonez
+1 Nice for "... where can you go from there? Where?"
David Robbins
David Robbins
A: 

Func only takes 4 args, and a TResult

Chris S
+8  A: 

FYI, the next version of the .NET libraries will include Func and Action generic types of more than four parameters.

Eric Lippert