如何设置命名的论据的String.format? [英] How to set named argument for string.Format?

查看:135
本文介绍了如何设置命名的论据的String.format?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有C#错误时调用:

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



该错误是的已规定所有固定参数之后命名参数规格必须出现

我该如何解决这个问题?

How can I fix this?

我更喜欢使用命名参数。

I prefer to use named parameters.

推荐答案

如果你想指定格式参数的名称,你必须指定下列说法也的名称:

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);



这不是非常有用,因为名称为arg0和ARG1没有在说什么所有关于论点。

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

另外,还有一些只有超载达人ARG2,所以如果你有多个参数,你必须把它们放入数组来命名参数:

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);

这篇关于如何设置命名的论据的String.format?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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