如何为 string.Format 设置命名参数?

发布于 2024-09-29 17:08:05 字数 188 浏览 1 评论 0原文

我在调用时遇到 C# 错误:

string.Format(format:"abbccc", 1,22);

错误是“命名参数规范必须在指定所有固定参数之后出现”

如何解决此问题?

[编辑]

我更喜欢使用命名参数。

I have C# error when calling:

string.Format(format:"abbccc", 1,22);

The error is "Named argument specifications must appear after all fixed arguments have been specified"

How can I fix this?

[Edit]

I prefer to use named parameters.

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

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

发布评论

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

评论(2

倦话 2024-10-06 17:08:05

如果要指定格式参数的名称,则还必须指定以下参数的名称:

string.Format(format:"abbccc", arg0:1, arg1:22);

这不是很有用,因为名称“arg0”和“arg1”根本没有说明有关参数的任何内容。

另外,只有“arg2”以下的重载,因此如果您有更多参数,则必须将它们放入数组中来命名参数:

string.Format(format:"abbccc", args:new object[] { 1, 2, 3, 4 });

您可以简单地跳过命名参数:

string.Format("abbccc", 1, 22);

If you want to specify the name of the format argument, you have to specify the name of the following argument also:

string.Format(format:"abbccc", arg0:1, arg1:22);

That's not very useful, as the names "arg0" and "arg1" doesn't say anything at all about the arguments.

Also, there are only overloads up to "arg2", so if you have more arguments, you have to put them in an array to name the argument:

string.Format(format:"abbccc", args:new object[] { 1, 2, 3, 4 });

You can simply skip naming the arguments:

string.Format("abbccc", 1, 22);
能怎样 2024-10-06 17:08:05

就我而言,我必须清理并重建解决方案,这使得错误消失。发生的事情是,我添加了一个像这样的参数

sched.ScheduleJob(Jobdetail:job, trigger); 

,我收到了触发器错误,所以我删除了 JobDetail:,语法错误消失了,但复杂化后我仍然收到错误

命名参数规范必须出现在所有固定参数之后
已指定

我清理了解决方案并重建,错误消失了。

In my case, I had to Clean and Rebuild the solution, that made the error go away. What happened was, I added an argument like this

sched.ScheduleJob(Jobdetail:job, trigger); 

I was getting an error for trigger, so I removed, JobDetail:, and the syntax error go away but on complication I was still getting the error

Named argument specifications must appear after all fixed arguments
have been specified

I cleaned the solution and rebuilt and the error went away.

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