Re: String.Format vs + operator

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



YES !!! Thank you, this is exactly what I have always thought but wanted to
confirm with other developers. Glad to hear I am not alone on this. Syntax
highligting rocks for this too.

JIM



"Morten Wennevik [C# MVP]" <MortenWennevik@xxxxxxxxxxx> wrote in message
news:op.tuhpefjedj93y5@xxxxxxxx
On Mon, 25 Jun 2007 20:18:41 +0200, Nicholas Paldino [.NET/C# MVP]
<mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:

James,

Using the + operator will be faster, as it will just pre-allocate the
string and copy the various elements together. With Format, the format
string is parsed, and that is going to require some overhead.

However, I do think that you should carefully consider the overhead
versus the readability/maintainability of the code, as I think that the
call
to Format is MUCH much better in this arena.

Of course, if you have a good number of these operations being
performed, then you could gain some performance by moving to the concat
operator (+).



In some cases I agree, but in many cases readability can be much improved by
not calling ToString(), in which case the OP's example narrows down to

string s = "yadda " + x + " yadda " + y;

which I would argue is more readable (syntax coloring will make it even more
readable) than

string s = string.Format("yadda {0} yadda {1}", x, y);


PS! If any of the parts of a string concatenation contains a string literal,
ToString() will automatically be called on other parts as well.

--
Happy coding!
Morten Wennevik [C# MVP]


.



Relevant Pages

  • Re: Quick Ctype Question
    ... Basically its showing how to use the format of the binding to upper the name ... vs using the CType function. ... Every object has a ToString() method. ... For a String object, ToString ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Quick Ctype Question
    ... and then the sub underneith it is as follows: ... Basically its showing how to use the format of the binding to upper the name ... Every object has a ToString() method. ... For a String object, ToString ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Guid.NewGuid - character
    ... I assume you mean if you call ToString() on the generated Guid. ... The format of the string returned by guid.ToStringis xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. ... Browse into any of the overloads that take in a format string to see the different formats that could be returned. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: String.Format vs + operator
    ... With Format, the format ... string is parsed, and that is going to require some overhead. ... versus the readability/maintainability of the code, as I think that the call ... If any of the parts of a string concatenation contains a string literal, ToString() will automatically be called on other parts as well. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Date format detection
    ... Specifies the locale for which the date string is to be formatted. ... date format for this locale. ... the system default-date format for the specified locale. ... be enclosed within single quotation marks in the date format picture. ...
    (borland.public.delphi.thirdpartytools.general)