Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between string and StringBuilder in C#

What is the difference between string and StringBuilder?

Also, what would be some examples for understanding?

like image 704
ratty Avatar asked Jun 18 '10 12:06

ratty


People also ask

What is the difference between StringBuilder and string?

StringBuilder is used to represent a mutable string of characters. Mutable means the string which can be changed. So String objects are immutable but StringBuilder is the mutable string type. It will not create a new modified instance of the current string object but do the modifications in the existing string object.

Is there a difference between string and StringBuilder in C#?

However, whereas a string is an immutable type in C#, StringBuilder is an example of a mutable item. In C#, a StringBuilder is a mutable series of characters that can be extended to store more characters if required.

What is the difference between the string class and the StringBuilder class?

A String is immutable in Java, while a StringBuilder is mutable in Java. An immutable object is an object whose content cannot be changed after it is created.

What are the differences between string and StringBuilder you need to list a few differences?

Then, as mentioned before, Strings are immutable while StringBuffer and StringBuilder are mutable. So, Strings cannot be changed when you use the String class; whereas Strings can change if you use the StringBuffer and StringBuilder class.


2 Answers

A string instance is immutable. You cannot change it after it was created. Any operation that appears to change the string instead returns a new instance:

string foo = "Foo"; // returns a new string instance instead of changing the old one string bar = foo.Replace('o', 'a'); string baz = foo + "bar"; // ditto here 

Immutable objects have some nice properties, such as they can be used across threads without fearing synchronization problems or that you can simply hand out your private backing fields directly without fearing that someone changes objects they shouldn't be changing (see arrays or mutable lists, which often need to be copied before returning them if that's not desired). But when used carelessly they may create severe performance problems (as nearly anything – if you need an example from a language that prides itself on speed of execution then look at C's string manipulation functions).

When you need a mutable string, such as one you're contructing piece-wise or where you change lots of things, then you'll need a StringBuilder which is a buffer of characters that can be changed. This has, for the most part, performance implications. If you want a mutable string and instead do it with a normal string instance, then you'll end up with creating and destroying lots of objects unnecessarily, whereas a StringBuilder instance itself will change, negating the need for many new objects.

Simple example: The following will make many programmers cringe with pain:

string s = string.Empty; for (i = 0; i < 1000; i++) {   s += i.ToString() + " "; } 

You'll end up creating 2001 strings here, 2000 of which are thrown away. The same example using StringBuilder:

StringBuilder sb = new StringBuilder(); for (i = 0; i < 1000; i++) {   sb.Append(i);   sb.Append(' '); } 

This should place much less stress on the memory allocator :-)

It should be noted however, that the C# compiler is reasonably smart when it comes to strings. For example, the following line

string foo = "abc" + "def" + "efg" + "hij"; 

will be joined by the compiler, leaving only a single string at runtime. Similarly, lines such as

string foo = a + b + c + d + e + f; 

will be rewritten to

string foo = string.Concat(a, b, c, d, e, f); 

so you don't have to pay for five nonsensical concatenations which would be the naïve way of handling that. This won't save you in loops as above (unless the compiler unrolls the loop but I think only the JIT may actually do so and better don't bet on that).

like image 199
Joey Avatar answered Oct 16 '22 23:10

Joey


String vs. StringBuilder

  • String

    • Under System namespace

    • Immutable (readonly) instance

    • Performance degrades when continuous change of value occurs

    • Thread-safe

  • StringBuilder (mutable string)

    1. Under System.Text namespace
    2. Mutable instance
    3. Shows better performance since new changes are made to an existing instance

For a descriptive article about this topic with a lot of examples using ObjectIDGenerator, follow this link.

Related Stack Overflow question: Mutability of string when string doesn't change in C#

like image 24
Shamseer K Avatar answered Oct 17 '22 00:10

Shamseer K