In my early .Net programming days, I used string.Format() only for complex string concatenations, for example to compile strings as
Problem with custome开发者_高级运维r order 234 of date 2/2/2002 and payment id 55543.
But now I use string.Format for almost every string concatenation I have to do, also simple ones such as prefixing a string with something.
Console.WriteLine(string.Format("\t\t{0}", myString));
Is there any possible overhead on this? Maybe I should use the regular +
operator to do these simple operations?
What's your opinion on this?
For simple string concatenations use the +
approach. It is clearer for simple things that don't require a format.
For more complex strings that have a certain format and where it is useful to retain the structure of the entire string and provide a placeholder for the input, use String.Format
.
And yes, there is an overhead. String.Format
uses a StringBuilder underneath the covers. Simple string concatenations will be much quicker in those scenarios. A couple of benchmarks and blog posts on this topic can be found quite easily. Of course it all depends on your usage. If small string concats are occurring in a loop then repeated usage of String.Format
will likely be more noticeable than a straightforward +
concat. If you are building up a large string in a loop then the classic example is to prefer StringBuilder
and related questions on concat versus StringBuilder can be found on SO.
EDIT: To clarify, this serves little purpose: String.Format("{0}{1}", a, b)
since there is not much formatting. It's simply a + b
. Unfortunately I've come across such examples in production code and as soon as I see String.Format I expect to see something that needs to be structured a certain way, not a straightforward concat.
OTOH, consider this phone number: "(" + area + ") " + number + " x" + extension
- there's too much going on and it's not easy to modify. In this case a String.Format is preferable: String.Format("({0}) {1} x{2}", area, number, extension)
. This is still a trivial example but you get the idea.
I also tend to use string.Format on most operation which need two or more strings/values to be combined as it produces easier to read code than starting and stopping strings with +'s in the middle.
To expand
string value = string.Format("Hello {0}", user.username);
is much more readable and expandable than
string value = "Hello" + user.username
for instance if you wanted to add the last login date as a system upgrade, you could simply expand the code to the following
string value = string.Format("Hello {0}, you last logged in {1}", user.username, user.lastLogin);
Simple concatenation is more efficient for simple things. Use String.Format()
for when things get more complicated and it makes your code easier to read.
I personally do the same thing (as long as the function I'm calling doesn't handle the formatting for me).
Clarrification
For regular string concatenation like "Hello " + "World!";
I would use a StringBuilder. Your example formats the string for output by prepending two tabs...which I consider more like formatting.
There is a difference between formatting and concatenation...be careful what you use for which.
String.Format()
uses a StringBuilder internally so the concatenation is going to be more efficient than doing regular string concatenation anyway.
You might want to change your example since Console.WriteLine()
can handle the formatting itself (no String.Format()
needed):
Console.WriteLine("\t\t{0}", myString);
My rule is if I have to use the +
(concatenation) more than once I change it to a string.Format
.
string a = "Something: " + x; // ok
string b = "Something: " + x + " something else"; // change it to a string.Format
string bB = string.Format("Something: {0} something else", x); // much nicer to read
string c = "Something: " + x + " " + y + " " + z; // Yuck
string cC = string.Format("Something: {0} {1} {2}", x, y, x); // Much nicer
I don't know about performance, someone will likely provide that data, but my feeling is that String.Format is the way to go if you want to put the format string in your config file (or database field or resource file or whatever). That way, if you want to tweak the number of tabs, switch to spaces, add delimiters, or whatever, you don't have to recompile.
I tend to use String.Concat
instead of String.Format
when I just need to prefix or suffix a given string. I prefer the explicit call to the Concat
method then to use the +
operator and if you are already use String.Format
it's just a matter of switching one keystroke.
IIRC, the String.Concat
method will be the same as using the operator so it also be faster than String.Format
.
I almost always use format, although I use an extension method instead of the static string method. I find it easier to understand, easier to change, and generally easier to maintain. It can also make localization easier because it doesn't introduce ordering issues the way concatenation does.
Honestly, which looks better?
"You have {0} widgets.".Frmt(widgetCount)
"You have " + widgetCount.ToString() + " widgets."
精彩评论