C # StringBuilder and String Analysis

Source: Internet
Author: User

C # is a common language. Here we mainly introduce C # StringBuilder and String, including StringBuilder, which is the best choice for many String concatenation or frequent operations on a String, do not use String or other features.

C #Difference between StringBuilder and String

A new instance is generated when String operations (such as value assignment and concatenation) are performed, while StringBuilder does not. Therefore, it is best to use StringBuilder when splicing a large number of strings or frequently performing operations on a String. Do not use String

In addition, we have to say a few words about StringBuilder and String:

1. It is a reference type and memory is allocated on the stack.

2. A new instance is generated during the operation.

3. Immutable)

4. Define equal operators (= and! =) Is used to compare the value of a String object (not a reference ).

C #StringBuilder and String examples:

 
 
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Text;
  4. Namespace Example22
  5. {
  6. Class Program
  7. {
  8. Static void Main (string [] args)
  9. {
  10. Const int cycle= 10000;
  11. Long vTickCount = Environment. TickCount;
  12. String str = null;
  13. For (int I = 0; I< Cycle; I ++)
  14. Str + = I. ToString ();
  15. Console. WriteLine ("String: {0} MSEL", Environment. TickCount-vTickCount );
  16. VTickCount = Environment. TickCount;
  17. // I am angry when I see this variable name. Why do everyone make it? :)
  18. StringBuilder sb = new StringBuilder ();
  19. For (int I = 0; I< Cycle; I ++)
  20. Sb. Append (I );
  21. Console. WriteLine ("StringBuilder: {0} MSEL", Environment. TickCount-vTickCount );
  22. String tmpStr1 = "";
  23. String tmpStr2 = tmpStr1;
  24. Console. WriteLine (tmpStr1 );
  25. Console. WriteLine (tmpStr2 );
  26. // Note that the value of tmpStr1 does not affect the value of tmpStr2.
  27. TmpStr1 = "B ";
  28. Console. WriteLine (tmpStr1 );
  29. Console. WriteLine (tmpStr2 );
  30. Console. ReadLine ();
  31. }
  32. }
  33. }

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.