命名参数调用C#中的变量参数号 [英] Named arguments call in C# with variable parameter number

查看:206
本文介绍了命名参数调用C#中的变量参数号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有下面的C#功能:

Suppose I have the following C# function:

void Foo(int bar, params string[] parpar) { }

我想用命名参数来调用这个函数:

I want to call this function using named arguments:

Foo(bar: 5, parpar: "a", "b", "c");



编译器给出的错误信息:前B命名的参数不能位于位置,因为我没有名字和c。

The compiler gives error message: "Named arguments cannot precede positional" since I have no name before "b" and "c".

有没有办法使用命名的参数,而无需手动代表PARAMS作为一个数组?

Is there any way to use named arguments without manually representing params as an array?

推荐答案

没有,没有语法糖使命名,除了明确指定数组变量的参数。

No, there is no syntactic sugar to make variable arguments named except explicitly specifying array.

注意 PARAMS 参数需要被单独命名如果这种语法将被允许(见,其中位于论点结束),但是只有一个名称

Note that params arguments would need to be individually named if such syntax would be allowed (to see where positioned argument ends), but there is only one name.

这篇关于命名参数调用C#中的变量参数号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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