Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 21st, 2007, 06:15 AM
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default String Mystery

I am confused between String And StringBuilder after reading an amazing article.

http://dotnetspider.com/kb/Article3378.aspx

I do not understand that the author explains the right or wrong idea.

Can anyone help me and tell that what is the best.

 
Old September 21st, 2007, 06:25 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Well as with all things in programming there are almost no definites but in general if you are building a long string from many parts then using the StringBuilder class is better.
If you've got a particular instance where you can't decide then post it here although performance measuring is hard, you need to know in what circumstances the code is run, web or desktop, multiple users or one at a time, processor count, threading etc.
Also String has many methods that StringBuilder doesn't so you don't always have a choice.

--

Joe (Microsoft MVP - XML)
 
Old September 21st, 2007, 08:12 AM
Authorized User
 
Join Date: Nov 2006
Posts: 93
Thanks: 0
Thanked 1 Time in 1 Post
Default

I thought the article was pretty straight forward. In a nutshell...

If you are building a string from user input use a String because the user won't be able to type fast enough to see a difference.

If you are sifting through a large amount of data, say, scanning for a specific word or character, use StringBuilder to concat the resulting string.

One other thing, StringBuilder will expose the underlying array of characters for editing where String will not. (There is the Chars array but it's read only). So, if you wanted to scan a string and replace certain characters you want StringBuilder.


What you don't know can hurt you!
 
Old October 6th, 2007, 04:19 PM
Authorized User
 
Join Date: Dec 2004
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to DZukiewicz
Default

I wrote an article about this a while ago:

http://blogs.bdnet.co.uk/dominicz/ar...atenation.aspx

Its a huge performance benefit too.

Regards,

Dom





Similar Threads
Thread Thread Starter Forum Replies Last Post
for-each looping - mystery comma return Navy1991_1 XSLT 6 June 5th, 2008 01:52 PM
error mystery htummala Oracle 2 February 28th, 2005 02:28 AM
Chapter 12 uploading mystery Graham Long BOOK: Beginning ASP 3.0 2 March 18th, 2004 04:10 PM
MM:DataSet mystery bridon123 Dreamweaver (all versions) 2 January 12th, 2004 10:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.