params 关键字是否保证项目的顺序与列出的顺序相同?
我正在开发一个简单的 API,它将接受许多 IBehaviours,然后将其应用到配置中。我使用 params 关键字进行设计,因为通常只需要一种行为,但有时需要更多行为。
然而,以正确的顺序应用行为非常重要。
public void Configure(string wow, params IBehaviour[] behaviours) { ... }
Configure("oh yes", new MustHappenFirst(), new MustHappenSecondly());
这是否
技术上意味着
行为
以相同的顺序发生时 列举? (按照标准, 不仅仅是实践上的明智)。在语义和直觉上传达相同的行为?
谢谢。
I'm working on a simple API that will accept a number of IBehaviours which are then applied in configuration. I am designing this using the params keyword since often there is just one behaviour wanted, but sometimes more.
However, it is very important that behaviours are applied in the correct order.
public void Configure(string wow, params IBehaviour[] behaviours) { ... }
Configure("oh yes", new MustHappenFirst(), new MustHappenSecondly());
Does this
Technically imply that
behaviours
occurs in the same order when
enumerating? (as in standard-wise,
not simply practically-wise).Semantically and intuitively convey that same behaviour?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
参数的计算将以从左到右的顺序进行,并且它们将按该顺序放入数组中。
请注意,如果“null”是行为的有效值,您可能会遇到麻烦:
调用
时不要
小心。
The evaluation of the arguments will happen in left-to-right order and they'll be put into the array in that order.
Note that if "null" is a valid value for a behaviour, you can get into trouble:
calls
not
so be careful with that.
是的。当您使用 params 时,编译器只是按照参数在程序中列出的顺序将参数转换为数组。它们在方法内始终按此顺序排列。
调用:
将始终映射到:
Yes. When you use
params
, the compiler just turns the arguments into an array, in the same order that the arguments were listed in the program. They will always be in this order inside the method.A call to:
Will always map to:
是的。使用 params 关键字为方法指定的参数将按照在方法调用中指定的顺序放置在 params 数组中。开发人员通常会理解这一点,他们熟悉 String.Format 等使用参数的方法,其中参数列表中格式化值的顺序非常重要。
Yes. Parameters specified for a method using the params keyword will be placed in the params array in the order they are specified in the method call. This is generally understood to be so by developers, who are familiar with params-using methods like String.Format where the order of a formatting value in the parameter list is very important.
是的;顺序被保留。
是的;这很好。
Yes; order is preserved.
Yes; this is fine.