First, readability is the main reason to use it. Which code is easier to
read?
string s = "<input type=\"";
s += Type;
s += "\" id=\";
s += ID;
s += "\" />";
or
string s = String.Format( @"<input type=""{0}"" id=""{1}"" />", Type, ID );
As far as performance, like Brock said, String.Format using StringBuilder to
build the new string. This is fine when you are concatenating several
strings, but performance on String.Format( "hello{0}, " world" ) will be
better to use string concatenation. StringBuilders aren't free, plus the
StringBuilder still has to parse the string out.
If you are really searching for optimally fast code, you should declare a
StringBuilder and use Append to add the values. But it is a balance between
readability and performance.
System.Text.StringBuilder sb = new StringBuilder( 50 ); //note the initial
size has been set, estimate the total size.
sb.Append( "<input type=\"" );
sb.Append( Type );
sb.Append( "\" id=\" );
sb.Append( ID );
sb.Append( "\" />" );
bill
"mavrick101" <ma********@discussions.microsoft.com> wrote in message
news:68**********************************@microsof t.com...
Why is String.format better than
just concatenating the string?
is it just the cost of creating a new string? or any thing more?