简体   繁体   English

使用“params”关键字的方法的几个重载

[英]Several overloads for method with “params” keyword

I've had a look at Path.Combine and noticed it has four overloads: 我已经看过Path.Combine ,发现它有四个重载:

  1. string , string stringstring
  2. string , string , string stringstringstring
  3. string , string , string , string stringstringstringstring
  4. params string[]

How are the first three overloads useful? 前三个重载如何有用?
The way I see it, the fourth overload makes the others pretty pointless. 我看到它的方式,第四次超载使其他人毫无意义。 I looked at the source and I did see that the fourth overload's implementation is a bit different, but even in this case I would expect to have just the one params overload which decides which implementation to use based on the array's length. 我查看了源代码,我确实看到第四个重载的实现有点不同,但即使在这种情况下,我也希望只有一个params重载,它根据数组的长度决定使用哪个实现。

According to this answer, https://stackoverflow.com/a/2796763/385844 , it's to avoid the overhead of creating the parameter array, and because the non-params overloads are convenient for users of languages that do not support variable-length parameter lists. 根据这个答案https://stackoverflow.com/a/2796763/385844 ,这是为了避免创建参数数组的开销,并且因为非params重载对于不支持可变长度的语言的用户来说很方便参数列表。

See also 也可以看看

Why does string.Format come in several flavors? 为什么string.Format有几种风格?

Just like Oded said, I found out that it must have been there for backward compatibility as I couldn't found it in 2.0, 3.5 就像Oded说的那样,我发现它必须存在向后兼容性,因为我在2.0,3.5中找不到它

I think the overloaded started in 4.0 我认为重载开始于4.0

As for the other many overloads, I wouldn't speak for .net team, but I feel they feel is pointless increasing the overloads every time so they stopped at 4 and provided an Array of string for more than 4 string combinations - which I think is wise 至于其他很多重载,我不会代表.net团队,但我觉得他们觉得每次增加重载都没有意义所以他们停在4并且提供了超过4个字符串组合的字符串数组 - 我认为是明智的

I based my explanation on Lambda expression where the team stopped at 16 arguments 我的解释基于Lambda表达式,团队停在了16个参数上

Action(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) 动作(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15,T16)

Path.Combine could have been like that but is pointless. Path.Combine可能就是这样,但毫无意义。

I can only speak from my experience with other C# developers. 我只能说我与其他C#开发人员的经验。

Not all developers are familiar or comfortable with the params syntax (and the fact that the technical name is variadic function parameters doesn't help). 并非所有开发人员都熟悉或熟悉params语法(以及技术名称为可变参数函数参数的事实并没有帮助)。
I know I've had to explain it over and over again, so it is not unusual to see calls 我知道我必须一遍又一遍地解释它,所以看到电话并不罕见

instance.ParamsMethod(new int[]{1});
//or even
instance.ParamsMethod(new List<int>{1}.ToArray());

for a method writen as: 对于一个写为:

public void ParamsMethod(params int[] source) {} public void ParamsMethod(params int [] source){}

negating all the sweet syntactic sugar of params (and then some). 否定了params所有甜的语法糖(然后是一些)。

So, my personal preference is to provide the 1 and 2 parameter case as overloads, because that somewhat makes it harder to clutter the code unnecessarily. 所以,我个人的偏好是提供1和2参数的情况作为重载,因为这有点使得更难以不必要地混乱代码。 The call is marginally slower because of the overload chaining, but it helps make clearer code. 由于过载链接,调用稍微慢一点,但它有助于使代码更清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM