Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stringbuilder.tostring() always immutable? 1

Status
Not open for further replies.

kmfna

MIS
Sep 26, 2003
306
US
Hello all,

I was curious, does C# make an immutable string every time you use the .ToString() method? IE. if you just use it for a message box or to pass to a function? Since immutable strings are difficult for c# to clean up (if it can at all) I would like to use as few actual string objects as possible...I know that for validation for my text boxes I will probably still have to, but I would really like to minimize the wasted memory....the program we are working on is going to run from a server with a LOT of resources, but there are going to be a LOT of users also...and even using like 35 megs or so per instance is going to be pushing it a bit.....so on that note, any other nice memory saving features you guys can think of and offer as advice would be great.

Thanks a lot, you are all great!
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
does C# make an immutable string every time you use the .ToString() method? "
Change "C#" to ".Net FrameWork" and the answer is yes, if the StringBuilder has changed since the last ToString. If it has not changed, I do not know.


Forms/Controls Resizing/Tabbing
Compare Code
Generate Sort Class in VB
Check To MS)
 
gotcha......thanks!

Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
does C# make an immutable string every time you use the .ToString() method? "
As John said, the answer is yes and it is true in any managed code including C#, VB.NET, C++ managed.
Use string when :
- declare a field or a property of 'string' type
- as a return type from a method
- passed as parameters to a method
- avoid to use string in loops like:
Code:
string test = "TESTSTRING1"
   string charstr = "";
   string numstr = "";
   foreach (char c in test)
   {
       if (char.IsNumber(c) == true)
       {
           numstr += c;
       }
       else
       {
            charstr += c;
       }
   }
The above code is working well but it is not recomendeded because immutable property.
-use StringBuilder always when there is a need to modify a string
There is no penalty when using:
Code:
 StrinBuilder sb=...;
 string str = sb.ToString();
If str is expected to be modified then is better to perform the modification on the sb object and extract the string at the end.
You can create an immutable type by using the ImmutableObjectAttribute class.

obislavu
 
Excellent....I wasn't quite sure about that, but had a hunch....by the way, nice code snippet, come from anyone I know? :) Here's a question though, can you create a stringbuilder typed function, so that you can just pass SBs around without having to use an actual string until absolutely necessary?

Thanks,
Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Thanks that cleared things up.

Have a star.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
can you create a stringbuilder typed function"

Of Course you can. Its just another reference type.
 
oh, yeah, I forgot to mention that I had already done it...I just wasn't entirely sure...however it works, so its all good.

Thanks though ST

Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top