为什么 Funcs 不接受超过 16 个参数? [英] Why don't Funcs accept more than 16 arguments?
问题描述
由于 Javascript 是我最精通的语言,所以我熟悉使用函数作为一流的对象.我原以为 C# 缺少这个功能,但后来我听说了 Func
和 Action
和 delegate
,我认为它们非常棒.>
例如,您可以声明一个 Func
连接两个字符串并在它们之间放置一个空格,如下所示:
Func<字符串,字符串,字符串>concat = (a,b) =>a + " " + b;
我注意到当你打字时
Func<
IntelliSense 显示它有 17 个重载:
委托 System.Func委托 System.Func委托 System.Func...剪...委托 System.Func<在 T1、T2、T3、T4、T5、T6、T7、T8、T9、T10、T11、T12、T13、T14、T15、在T16中,出TResult>
这让我笑了.我查看了 Func 的 MSDN 文档代码> 又笑了.这让我尝试用 17 个参数声明一个
Func
.它会导致错误(使用泛型类型System.Func
).
我同意拥有一个接受超过 16 个参数的 Func
可能不是一个好主意.即便如此,对于 Func
来说,这似乎是一种笨拙的实现方式.它需要记录 17 个不同的重载.这就是它真正需要知道的:最后一个类型参数是返回类型,它之前的所有类型参数都是参数类型.
如果我想创建一个超过 16 个参数的 Func
该怎么办?为什么总有限制?为什么 C# 不能让你声明一个带有任意数量参数的 Func
?
你希望像 variadicC# 缺少的类型参数.C# 需要固定泛型类型的数量,因此 Func
、Action
和 Tuple
类型的数量令人发指.
如果您正在购买语言,此功能是在 C++11 中添加的,但您可能应该只使用 jQuery.:-)
Since Javascript is the language that I am the most proficient at, I am familiar with using functions as first-class objects. I had thought that C# lacked this feature, but then I heard about Func
and Action
and delegate
, which I think are pretty awesomesauce.
For example, you can declare a Func
that concatenates two strings and puts a space between them like this:
Func<string, string, string> concat = (a,b) => a + " " + b;
I noticed that when you type
Func<
the IntelliSense shows that it has 17 overloads:
delegate System.Func<out TResult>
delegate System.Func<in T, out TResult>
delegate System.Func<in T1, in T2, out TResult>
...snip...
delegate System.Func<in T1, in T2, in T3, in T4, in T5, in T6, in T7, in T8, in T9, in T10, in T11, in T12, in T13, in T14, in T15, in T16, out TResult>
That made me laugh. I looked at the MSDN docs for Func
and laughed again. This made me try to declare a Func
with 17 arguments. It causes an error (Using the generic type 'System.Func<TResult>' requires 1 type arguments
).
I can agree that it's probably not a good idea to have a Func
that accepts more than 16 arguments. Even so, this seems like a kludgy way for Func
to be implemented. It requires 17 trivially different overloads to be documented. This is all it really should need to know: the last type parameter is the return type, and all the type parameters before it are the argument types.
So what could I do if I wanted to create a Func
with more than 16 parameters? Why is there a limit anyway? Why can't C# just let you declare a Func
with an arbitrary number of arguments?
You're hoping for something like variadic type arguments which C# lacks. C# requires the arity of generic types to be fixed, therefore the heinous proliferation of Func
, Action
, and Tuple
types.
If you're language shopping, this feature was added in C++11, but you should probably just use jQuery. :-)
这篇关于为什么 Funcs 不接受超过 16 个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!