开发者

Efficiency of Indexed Placeholders

There are several places where you can use the indexed placeholder syntax in C#, ie.

// Assume some object is available with 2 string properties
Console.Writeline("Hello {0}, today is {1}", obj.Us开发者_如何学JAVAername, obj.DayOfWeek);

Is that more efficient than using the string concatenation operator to build the string? ie.

Console.Writeline("Hello " + obj.Username + " today is " + obj.DayOfWeek);

Obviously the {0} ... {n} syntax is cleaner if you're doing something complicated -- but which code is more efficient (lower memory footprint and or execution time?)


Well the first version has to parse the string and interpret it, before doing the actual string concatenation. So one would expect the first method to be slower, and potentially more memory-intensive, no?

But unless you're doing vast amounts of string processing, it's unlikely to be an issue.


Don't think about it, use the formatting one. If you worry about memory/execution time with such a method, you have other problems.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜