Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2006

Tip: Looking for answers? Try searching our database.

Fixed string concatenation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Glenn - 04 Jul 2006 11:51 GMT
Hi

I realise StringBuilder is generally better for building strings
dynamically, but for a fixed string built over a number of lines, would the
compiler recognise that the following could be represented as a single
string and optimise out reallocations?

Example

string myString = "Blah";
myString += "Blah";
myString += "Blah";
myString += "Blah";
myString += "Blah";

Thanks

Glenn
Phill W. - 04 Jul 2006 13:30 GMT
> would the compiler recognise that the following could be represented
> as a single string and optimise out reallocations?
[quoted text clipped - 4 lines]
> myString += "Blah";
> myString += "Blah";

Try it and see - the ILDasm utility is your friend in this - but my
/guess/ is that it won't.

You could, of course, rewrite the above as ...

string myString
 = "Blah"
 + "Blah"
 + "Blah"
 + "Blah"
 + "Blah" ;

... which the compiler /does/ optimise - at least the equivalent
construct in VB.Net gets optimised so the C# compiler d*** well ought to
as well....   ;-)

HTH,
   Phill  W.
Jon Skeet [C# MVP] - 04 Jul 2006 18:43 GMT
> I realise StringBuilder is generally better for building strings
> dynamically, but for a fixed string built over a number of lines, would the
[quoted text clipped - 8 lines]
> myString += "Blah";
> myString += "Blah";

I don't believe it does - but if you write it as:

string myString = "Blah"
                +"Blah"
                +"Blah"
                +"Blah"
                +"Blah";

then the concatenation will be done at compile time.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.