antonioh
antonioh

Reputation: 2944

Dictionaries and Lambdas fun

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

Upvotes: 6

Views: 747

Answers (4)

Eric Lippert
Eric Lippert

Reputation: 660377

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

Upvotes: 10

Chris S
Chris S

Reputation: 65446

Func only takes 4 args, and a TResult

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1502406

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?

Upvotes: 18

RossFabricant
RossFabricant

Reputation: 12492

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.

Upvotes: 3

Related Questions