string is immutable and stringbuilder is mutable
Asked Answered
V

8

2

can any one explain with examples


Related Discussion: Most efficient way to concatenate strings?

Violate answered 20/3, 2009 at 9:42 Comment(1)
Classic .NET Interview questionFlake
L
7

You cannot edit the value of a string, each method of the string object returns a new string instead of altering the original. StringBuilder on the other hand can alter it's content (adding new strings for example).

string original = "the brown fox jumped over the lazy dog";
string altered = original.Insert(original.IndexOf("fox"), "cat");
// altered = the brown cat jumped over the lazy dog

You cannot change the content of the original string unless you create a new string, or re-reference the instance to another string object.

Lock answered 20/3, 2009 at 9:50 Comment(1)
altered should look like the brown catfox jumped over the lazy dog.Seaware
Q
17
string s = "a";

s += "b"; // this will create a new string object

StringBuilder sb = new StringBuild();
sb.Append("a");
sb.Append("b"); // appends to existing StringBuilder
Quintana answered 20/3, 2009 at 9:44 Comment(1)
That's a nice explanation..!!Wetmore
L
7

You cannot edit the value of a string, each method of the string object returns a new string instead of altering the original. StringBuilder on the other hand can alter it's content (adding new strings for example).

string original = "the brown fox jumped over the lazy dog";
string altered = original.Insert(original.IndexOf("fox"), "cat");
// altered = the brown cat jumped over the lazy dog

You cannot change the content of the original string unless you create a new string, or re-reference the instance to another string object.

Lock answered 20/3, 2009 at 9:50 Comment(1)
altered should look like the brown catfox jumped over the lazy dog.Seaware
A
4

A string object is immutable, once created, it cannot be changed

string str;
//new string object constructed. str= new string("string1");
str="string1";
//again new object will be constructed str=new string("string1string2");
str=str+"string2" 
since a new object is created for every assignment, there is an overhead.

However, string builder class provides an efficient way to repeatedly append bits of string to already constructed object.

StringBuilder str=new StringBuilder();
str.Append("string1");
str.Append("string2");

The performance difference will be too small to compare on fewer assignment and concatenation operation, but there is significance performance gain by switching from string to stringbuilder if we have more of these string operations.

Ailurophobe answered 20/3, 2009 at 10:8 Comment(0)
P
3

Even though an old question, thought I would still answer.

String (reference-type storage)

Puts the value of the string on the managed heap, and points to it, using a pointer. If you change the value of the string, the new value is stored on the managed heap, in another location, and the pointer is changed to point to the new location. The old value remains on the heap, without any pointer to its location. Eventually one can run out of heap memory.

This is a security safeguard. It ensures that unless the programmer physically alters the string (actually putting a new value on the stack and changing the pointer), the string cannot be altered by the system. I don't think there is a way to change the pointer from the new value on the heap, to the old value on the heap.


Edited 10-20-2010

StringBuilder (value-type storage) <<< INCORRECT

Does not use the managed heap, and therefore, does not use a pointer to a value. It creates a value, in a single location. The value in that single location can be manipulated in many different ways.

StringBuilder (reference-type storage)

Puts an object on the managed heap, and points to it, using a pointer. The value is placed in the object by way of the pointer. The value can be manipulated by way of the pointer. I think this means that if the value is changed, address space on the heap is allocated to accommodate the expansion or contraction of the value.


The Trade Off

Stringbuilder uses negligibly more processing time, less memory for storage [on the heap], and its values can be byte-wise manipulated.

String uses negligibly less processing time, and more memory for storage, because it cannot change its old values, nor can it dispose of them. Assigning a new value to a string simply directs the pointer to another place on the heap, and stores the new value there. Old values stay on the heap forever, without pointers.

Comparison of reference-type storage and value-type storage.

alt text

Rule of Thumb

  • Anything inside System.ValueType, is a value type.
  • Anything outside system.ValueType, is a reference type.

Finally, an article showing some code tests to prove these points.

Patino answered 14/10, 2010 at 4:4 Comment(2)
This is incorrect. StringBuilder is a reference type and thus instances will always be stored on the heap. Please see msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspxQuintana
"Old values stay on the heap forever" - only until they are garbage collectedGiraldo
R
2

In C++ you could do this:

std::string hello = "Hello, world!\r\n";
hello[7] = 'W';

In C#, you can't do this. Strings can't be changed.

Recorder answered 20/3, 2009 at 10:6 Comment(0)
I
1

Some of the reasons why they have been made immutable:

  • Immutable types are thread safe meaning you can pass a string around without worrying it's been changed.
  • Security is much more of a concern if strings can be changed.
Isomeric answered 20/3, 2009 at 10:5 Comment(0)
E
0

String is immutable ,,

That means we cannot change the string once declared .

String builder is mutable

This is the variety operation can be performed.we can change string ..

Expeditious answered 26/4, 2009 at 12:41 Comment(0)
H
0

Immutability Of String[instance is readonly]

    string s="hi";
    //now let s be address 1000
    s+=" there"; 
    //above concatination causes creation of new instance in address location other than 1000
    //with modified value "hi there"

Mutability Of stringbuilder[instance can be modified]

    StringBuilder sbr = new StringBuilder("My Favourite Programming Font is ");
    sbr.Append("Inconsolata")
//append operation only modifies existing instance of sbr rather than creating new instance

For more Interesting examples and detailed discussions Strongly recommend to Follow this link

String Vs StringBuilder

  • String
    1. under System namespace
    2. Immutable(readonly) instance
    3. performance degrades when continuous change of value occures
    4. thread safe
  • StringBuilder(mutable string)
    1. under System.Text namespace
    2. mutable instance
    3. shows better perfomance since new changes are made to existing instance

Descriptive article about this topic with lot of examples using ObjectIDGenerator,follow this link

Related Stackeoverflow Question: Mutability of string when string doesn't change in C#?

Hallowell answered 26/2, 2015 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.