Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 481
antonioh Avatar asked Apr 08 '09 15:04

antonioh


3 Answers

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?

like image 97
Jon Skeet Avatar answered Nov 12 '22 17:11

Jon Skeet


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

like image 35
Eric Lippert Avatar answered Nov 12 '22 18:11

Eric Lippert


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.

like image 40
RossFabricant Avatar answered Nov 12 '22 18:11

RossFabricant