params 关键字是否保证项目的顺序与列出的顺序相同?

发布于 2024-11-15 00:12:43 字数 432 浏览 3 评论 0原文

我正在开发一个简单的 API,它将接受许多 IBehaviours,然后将其应用到配置中。我使用 params 关键字进行设计,因为通常只需要一种行为,但有时需要更多行为。

然而,以正确的顺序应用行为非常重要。

public void Configure(string wow, params IBehaviour[] behaviours) { ... }

Configure("oh yes", new MustHappenFirst(), new MustHappenSecondly());

这是否

  1. 技术上意味着行为 以相同的顺序发生时 列举? (按照标准, 不仅仅是实践上的明智)。

  2. 在语义和直觉上传达相同的行为?

谢谢。

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

  1. Technically imply that behaviours
    occurs in the same order when
    enumerating? (as in standard-wise,
    not simply practically-wise).

  2. Semantically and intuitively convey that same behaviour?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

巡山小妖精 2024-11-22 00:12:43

参数的计算将以从左到右的顺序进行,并且它们将按该顺序放入数组中。

请注意,如果“null”是行为的有效值,您可能会遇到麻烦:

Configure("hello", null);

调用

Configure("hello", (IBehaviour[]) null);

时不要

Configure("hello", new IBehaviour[1] { 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:

Configure("hello", null);

calls

Configure("hello", (IBehaviour[]) null);

not

Configure("hello", new IBehaviour[1] { null } );

so be careful with that.

孤君无依 2024-11-22 00:12:43

是的。当您使用 params 时,编译器只是按照参数在程序中列出的顺序将参数转换为数组。它们在方法内始终按此顺序排列。

调用:

Configure("wow", one, two, three);

将始终映射到:

Configure("wow", new[] {one, two, three});

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:

Configure("wow", one, two, three);

Will always map to:

Configure("wow", new[] {one, two, three});
指尖上得阳光 2024-11-22 00:12:43

是的。使用 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.

等待我真够勒 2024-11-22 00:12:43
  1. 是的;顺序被保留。

  2. 是的;这很好。

  1. Yes; order is preserved.

  2. Yes; this is fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文