2

can any one explain with examples


Related Discussion: Most efficient way to concatenate strings?

Community
  • 1
  • 1
peter
  • 8,158
  • 21
  • 66
  • 119

8 Answers8

16
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
Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
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.

Björn
  • 29,019
  • 9
  • 65
  • 81
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.

cgreeno
  • 31,943
  • 7
  • 66
  • 87
fitnet
  • 93
  • 10
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.

Michael
  • 1,340
  • 1
  • 11
  • 7
  • This is incorrect. StringBuilder is a reference type and thus instances will always be stored on the heap. Please see http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx – Brian Rasmussen Oct 20 '10 at 07:33
  • "Old values stay on the heap forever" - only until they are garbage collected – Matt Wilko Mar 10 '15 at 13:21
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.

Dave Van den Eynde
  • 17,020
  • 7
  • 59
  • 90
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.
cgreeno
  • 31,943
  • 7
  • 66
  • 87
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#?

    Community
    • 1
    • 1
    Shamseer K
    • 4,964
    • 2
    • 25
    • 43
    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 ..