can any one explain with examples
Related Discussion: http://stackoverflow.com/questions/21078/whats-the-best-string-concatenation-method-using-c
can any one explain with examples
Related Discussion: http://stackoverflow.com/questions/21078/whats-the-best-string-concatenation-method-using-c
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
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.
Some of the reasons why they have been made immutable:
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.
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.
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.
Rule of Thumb
Finally, an article showing some code tests to prove these points.
See here for some more reasons of String immutablility
http://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html