Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   The immutability of strings (http://www.programmingforums.org/showthread.php?t=15568)

Arla Apr 7th, 2008 7:41 PM

The immutability of strings
 
Quick question...

Just having read through stuff recently and trying to be a little more concious of efficiency in my programs I wasn't sure about this.

I know that having the following
:

String test = "James";
test += " is testing this";


Actually creates two strings, and it's better to use stringbuilder class to do this type of thing.

However, does this

:

String test = "James" + " is testing this";

Create one or two strings? I would assume one, but you know what they say about assuming!

Jimbo Apr 8th, 2008 12:16 AM

Re: The immutability of strings
 
It creates 3. One ("James"), two (" is testing this"), and a third to store the result ("James is testing this")

Nursing the efficiency of String vs. StringBuilder is often not going to get you much benefit. Of course, there are loops that are vastly improved by the change, but often it's easier to just use String.

OpenLoop Apr 8th, 2008 7:52 AM

Re: The immutability of strings
 
Quote:

Originally Posted by Jimbo (Post 143633)
Nursing the efficiency of String vs. StringBuilder is often not going to get you much benefit.

..not to mention that if efficiency is critical to your application, you should use a native (non=managed) language like C/C++.

xavier Apr 8th, 2008 12:41 PM

Re: The immutability of strings
 
I guess someone had to say that :)

Arla Apr 8th, 2008 2:56 PM

Re: The immutability of strings
 
Thanks for the info Jimbo, very useful, I had already played some with StringBuilder for the scrabble program just because having the number of string operations that I had (for 8!) was a HUGE overhead, changing to stringbuilder vastly improved performance.

Arla Apr 8th, 2008 2:58 PM

Re: The immutability of strings
 
Quote:

Originally Posted by OpenLoop (Post 143639)
..not to mention that if efficiency is critical to your application, you should use a native (non=managed) language like C/C++.

Efficiency isn't critical, but I like to try to program in an efficient manner, taking 10 seconds to do something that should take less than a second (if programmed in the same language) is bad practice.


All times are GMT -5. The time now is 4:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC