为什么不Funcs接受超过16个参数? [英] Why don't Funcs accept more than 16 arguments?

查看:135
本文介绍了为什么不Funcs接受超过16个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Javascript是我最熟练的语言,因此我熟悉将函数用作一等对象。我曾想过C#缺乏这个功能,但后来我听说 Func Action 例如,你可以声明一个 Func ,我认为它是非常棒的。 c>,它连接两个字符串并在它们之间放置一个空格,如下所示:

  Func< string,string,string> concat =(a,b)=> a ++ b; 

我注意到当你输入

 函数功能< 

智能感知表明它有17个重载:

 委托System.Func< out TResult> 
委托System.Func<在T,out TResult>
代表System.Func<在T1中,在T2中,out TResult>
... snip ...
委托System.Func

这让我发笑。我查看了 MSDN文档 Func 再次笑了起来。这让我尝试用17个参数声明 Func 。它导致一个错误(使用泛型类型'System.Func< TResult>'需要1个类型参数)。



我同意,有一个接受16个以上参数的 Func 可能不是一个好主意。即使如此,这对于执行 Func 来说似乎也是一种非常方便的方式。它需要17个不同的重载记录。这是它真正需要知道的一切:最后一个类型参数是返回类型,并且它之前的所有类型参数都是参数类型。



如果我想用超过16个参数创建 Func ?为什么有一个限制呢?为什么C#不能让你用任意数量的参数声明一个 Func

解决方案

您希望像C#所缺少的可变类型参数。 C#需要修改泛型的类型,因此,可能会出现令人讨厌的扩散 Func Action 元组类型。



如果您是语言购物用户,此功能已添加到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆