的String.Format参数顺序烦恼 [英] String.Format parameter order annoyance
问题描述
这真是烦人C#怎么好像强迫你明确地命名的String.Format每一个参数的索引,如果你想在某个地方添加其他参数,您可能已重新建立索引的字符串或把你的新参数在最后。
有没有办法让C#来自动执行此操作?
例如。 (我知道这是没有意义的学究,它只是一个例子:)
我开始:
的String.Format({0} {1} {1} {2} {3},A,b,C,D)
如果我想在开始添加一个参数,我可以执行下列操作之一:
的String.Format({4} {0} {1} {1} {2} {3},A,b,C,D,E)
的String.Format ({0} {1} {2} {2} {3} {4},E,A,b,C,D)
在Delphi比如我可以做这个等价的:
的String.Format ({} {} {} {2} {} {},E,A,b,C,D)
< DIV CLASS =h2_lin>解决方案
好了,没有什么在C#自动为您做到这一点。你总是可以编写自己的方法来做到这一点,但坦率地说我会找到它的可读性。还有很多更想去做(IMO)了解你的最后一行确实比前一个东西。当你点击 {2}
你必须在精神上原路返回,并与替换原来的项目{3}
来跳过 {2}
等。
我个人更喜欢代码,需要更长的时间打字,但很明显阅读。
it's really annoying how C# seems to force you to explicitly name the index of every parameter in String.Format, if you want to add another parameter somewhere you either have to re-index the string or put your new parameter at the end.
Is there a way to get C# to do this automatically?
e.g. (I know this is pointless pedants, it's just an example :)
I start with:
String.Format("{0} {1} {1} {2} {3}", a, b, c, d)
if I want to add a parameter at the beginning I can do one of the following:
String.Format("{4} {0} {1} {1} {2} {3}", a, b, c, d, e)
String.Format("{0} {1} {2} {2} {3} {4}", e, a, b, c, d)
in Delphi for example I could do the equivalent of this:
String.Format("{} {} {} {2} {} {}", e, a, b, c, d)
Well, there's nothing in C# to do this automatically for you. You could always write your own method to do it, but frankly I'd find it less readable. There's a lot more thinking to do (IMO) to understand what your final line does than the previous one. When you hit the {2}
you've got to mentally backtrack and replace the previous item with {3}
to skip the {2}
etc.
Personally I prefer code which takes a bit longer to type, but is clear to read.
这篇关于的String.Format参数顺序烦恼的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!