The difference between string and StringBuilder in C #

Source: Internet
Author: User

String has its peculiarities in any language, as is the case in. Net. It belongs to the base data type and is the only reference type in the base data type. A string can be declared as a constant, but it is placed in the heap.

One: non-changing objects

In. NET, a string is an immutable object, and once you create a string object and assign it a value, it cannot be changed, that is, you cannot change the value of a string. The beginning of this life sounds a bit strange, you may immediately think of string connection operation, we can not change the string? Look at the following code:

 public  static  void  Main (string  [] args) { string  s =  1234   " ;    Console.WriteLine (s); s  + =  5678   "     //  output the following result:  1234  12345678  

It seems that we have changed the value of s from "1234" to "12345678", and it doesn't actually change. string s = "1234"; is the creation of a string object whose value is "1234", s pointing to its address in memory, s + = "5678"; is creating a new string object whose value is "12345678" and s points to the new memory address. There are actually two string objects in the heap, although we only refer to one of them, but the string "1234" still resides in memory.

II: Reference type

As mentioned earlier, string is a reference type, and if we create many string objects of the same value, it should have the same point in memory as the address. That is, when we create the string object s, its value is "1234", when we create a string object with a value of "1234" STR, it will not allocate a chunk of memory space, but directly points to the s in memory address. This ensures efficient use of memory. Look at the following code:

 Public Static voidMain (string[] args) {    strings ="1234";    Console.WriteLine (s);    Change (s);    Console.WriteLine (s); Console.read ();} Public Static voidChange (stringstr) {STR="5678";}//output The following result:123412345678

Make a small change, pay attention to changes (ref string s)

 Public Static voidMain (string[] args) {    strings ="1234";    Console.WriteLine (s); Change (refs);    Console.WriteLine (s); Console.read ();} Public Static voidChange (ref stringstr) {STR="5678";}//output The following result:12345678
Three: StringBuilder object

As you can see from the above analysis, the string type is very inefficient in connection with strings, and because each connection operation creates a new object in memory that consumes a lot of memory space. This leads to the StringBuilder object, and the StringBuilder object is modified on the original string when the string connection operation improves performance. We may know this in our usual use, and it is recommended to use the StringBuilder object when the connection operation is frequent. But how big is the difference between the two? To do a test:

 Public Static voidMain (string[] args) {    strings =""; StringBuilder SB=NewStringBuilder (); intTimes =10000; intstart, end; //the time it took to test the stringStart =Environment.tickcount;  for(inti =0; I < times; i++) {s+=i.tostring (); } End=Environment.tickcount; Console.WriteLine (End-start); //the time it took to test the StringBuilderStart =Environment.tickcount;  for(inti =0; I < times; i++) {sb.    Append (i.ToString ()); } End=Environment.tickcount; Console.WriteLine (End-start); Console.read ();}//output The following result:8840

From the above analysis, it can be seen that string to make the connection of strings is very inefficient, but not under any circumstances to use StringBuilder, when we connect a few strings can be used string, but as a large number or frequent string connection operation, Be sure to use StringBuilder.

The difference between string and StringBuilder in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.