简体   繁体   English

方法签名中的params关键字到底是什么意思

[英]What does params keyword in method signature really mean

I'm skimming through Troelsen's Pro C# 2010 and came across the discussion of the params keyword method modifier. 我浏览了Troelsen的Pro C#2010,并遇到了有关params关键字方法修饰符的讨论。 Reading the text, MSDN, and other tubez sources, it seems to me the only thing you get from params is the ability to pass a comma delimited list of values to the method. 阅读文本,MSDN和其他tubez资料后,在我看来,从params上获得的唯一一件事就是能够将以逗号分隔的值列表传递给该方法。 I wrote some code to prove to myself that I can send arrays of different lengths to a method that does not use the params keyword, and everything works just fine. 我写了一些代码向我证明,我可以将不同长度的数组发送给不使用params关键字的方法,并且一切正常。

Now, I'm all in favor of saving keystrokes when it makes sense. 现在,我都支持在合理时保存击键。 In this case, however, I suspect the savings is illusory, and confusing to those who have to maintain code. 但是,在这种情况下,我怀疑这种节省是虚幻的,并且使那些必须维护代码的人感到困惑。 Illusory because I'm never going to send a hard-coded list of values to a method (bad form!). 虚幻的原因是,我永远不会将硬编码的值列表发送给方法(错误的形式!)。 Confusing because what's a maintainer to make of a method that lists a bunch of values rather than an object that explains what I'm doing? 令人困惑的是,维护者要使用列出一堆值而不是解释我在做什么的对象的方法来做什么?

OTOH, since the most of the folks at MS, and most of the folks here, are smarter than I am, I suspect I'm missing something. OTOH,因为MS的大多数人和这里的大多数人比我聪明,我怀疑我缺少了一些东西。 Please, anyone, enlighten me! 请任何人启发我!

Thanks. 谢谢。

Have you ever used Console.WriteLine ? 您曾经使用过Console.WriteLine吗?

Console.WriteLine("{0}: The value of {1} is {2}.", lineNumber, foo, bar);

This sort of call is where params can be useful. 这种调用是params可能有用的地方。 Having to explicitly construct an array to contain your parameters is just noise which makes the code harder to read without adding any value. 必须显式构造一个数组来包含您的参数只是噪音,这使得代码在不添加任何值的情况下很难阅读。 Compare to the how it would look if params weren't used: 与不使用params外观相比:

Console.WriteLine("{0}: The value of {1} is {2}.", new object[] { lineNumber, foo, bar });

The second version adds no useful information to the code, and moves the values further from the format string, making it harder to see what is going on. 第二个版本没有在代码中添加任何有用的信息,并且将值从格式字符串移到更远的位置,这使得查看情况变得更加困难。

Because it is easier for me to type: 因为我输入起来比较容易:

SomeFuncWithParams(a, b, c);

Than it is to type: 比键入:

SomeFuncWithParams(new object[] { a, b, c });

And if you think syntactic sugar is not a good reason to implement a feature then you'll also need to start questioning the validity of properties, the using statement, object initializers, etc. 而且,如果您认为语法糖不是实现功能的好理由,那么您还需要开始质疑属性, using语句,对象初始化程序等的有效性。

  1. Yes, I've always thought of the .Net "params" keyword as the moral equivalent of C/C++ variadic arguments. 是的,我一直认为.Net“ params”关键字在道德上等同于C / C ++可变参数。

  2. No, I can't think of any way that "params" and a variable-length array aren't completely equivalent. 不,我想不出“参数”和可变长度数组不完全等效的任何方式。

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

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