C# String.Format 与 ToString()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10400142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String.Format vs ToString()
提问by dougajmcdonald
Can anyone explain if there is any benefit in either one of the following methods:
任何人都可以解释以下任一方法是否有任何好处:
decimal d = 12.0m;
// 1. how I'd have done it
myLabel.Text = d.ToString();
// 2. how I saw someone do it today
myLabel.Text = String.Format("{0}", d);
Just to clarify, I'm not querying what the methods do, I'm obviously happy with that, just if there is perhaps a performance benefit in one over the other in this specific example. I'm aware of the added flexibility of cultures and formatting offered by string.format(), but I'd always just 'tostring()' numerics to attach their value to a label, or text based property in general.
只是为了澄清,我不是在询问这些方法的作用,我显然对此很满意,只是在这个特定示例中,一个方法可能比另一个方法有性能优势。我知道 string.format() 提供的文化和格式的额外灵活性,但我总是只用 'tostring()' 数字将它们的值附加到标签或一般基于文本的属性。
To me, the string.format() option seems like more typing for no additional benefit here, but I wondered if there are any other 'under the hood' benefits of doing things one way vs the other.
对我来说,string.format() 选项似乎更像是打字,没有额外的好处,但我想知道以一种方式做事与另一种方式做事是否还有其他“幕后”好处。
采纳答案by Dave Bish
I did a little benchmark in Linqpad:
我在 Linqpad 中做了一些基准测试:
void Main()
{
int iterations = 1000000;
decimal d = 12.0m;
var text = "";
var sw = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
{
// 1. how I'd have done it
text = d.ToString();
}
sw.Stop();
sw.ElapsedMilliseconds.Dump("ToString()");
sw = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
{
// 2. how I saw someone do it today
text = String.Format("{0}", d);
}
sw.Stop();
sw.ElapsedMilliseconds.Dump("Format");
}
ToString() 157
第 157 章
Format 264
格式 264
ToString() looks consistently faster.
ToString() 看起来始终更快。
EDIT: I should point out, that on my PC 10 million "Format" operations only took 2.2 seconds. This looks very much like a micro-optimization, and unless what you're doing is extremely performance-critical, or iterative - it'd not worry about this too much.
编辑:我应该指出,在我的 PC 上,1000 万次“格式化”操作只需要 2.2 秒。这看起来非常像一个微优化,除非你正在做的事情对性能非常关键,或者迭代 - 它不会太担心这个。
回答by Kendall Frey
ToString()is better, since Format()needs to call ToString()under the hood anyway. If you need to specify custom formatting, Format()offers format specifiers like alignment/padding. Otherwise, there is no reason not to use ToString().
ToString()更好,因为无论如何都Format()需要ToString()在引擎盖下打电话。如果您需要指定自定义格式,请Format()提供格式说明符,如对齐/填充。否则,没有理由不使用ToString().
ToString()fits much better into self-documenting code than Format(), and just makes more sense.
ToString()比 更适合自文档化代码Format(),而且更有意义。
回答by Royi Namir
string format - uses under the hood- StringBuilder- which is much faster for working with strings.
字符串格式 - 在幕后使用StringBuilder- - 处理字符串要快得多。
ToString is the default representation of an object.
ToString 是对象的默认表示。
回答by Tejs
string.Formatcan be used for creating other complex strings since it can take a paramsargument set; so this isn't really a question of apples VS apples.
string.Format可用于创建其他复杂字符串,因为它可以接受params参数集;所以这真的不是苹果对苹果的问题。
回答by Tim Rogers
I would expect there to be a marginal performance benefit of calling ToString()since it doesn't have to parse your format string. That, and it reads much more nicely if you are not using any features of features of Format().
我希望调用会带来边际性能优势,ToString()因为它不必解析您的格式字符串。如果您不使用Format().

