The characteristics of the reference type string stringbuilder class

Source: Internet
Author: User

String is a reference type, but it also has someValueType features.

String indicates whether to pass the value or reference. The string declaration of c # Is a string class. Of course, it is to pass the reference. However, most of these questions are due to this situation:
String A = "AAA ";
String B =;
B = "BBB ";

At this time, the output result shows that the value of a has not changed. analyze it as follows:

String A = "AAA"; // A --> New String ("AAA ")
String B = A; // B --> A, upload reference
B = "BBB"; // B --> New String ("BBB"), passed for reference. B points to a new string with the new string, and a does not change.

Stringbuilder is a reference type. If you need to operate strings, stringbuilder is recommended. For example, you need to constantly concatenate strings. System. text must be referenced

The string object cannot be changed. Every time you use a method in the system. string class, you must create a new String object in the memory, which requires a new space for the new object. If you need to modify the string repeatedly, the system overhead associated with creating a new String object may be very expensive. If you want to modify the string without creating a new object, you can use the system. Text. stringbuilder class. For example, when many strings are connected together in a loop, using the stringbuilder class can improve performance.

Class reference is type. No matter how the value is passed, the reference is transmitted.

The following is a simple example:

Namespace TCP
{
Public   Class Program
{
Static   Void Main ( String [] ARGs)
{
String Code =   " C # " ;
User user =   New User ( " Li " , " 23 " );
Stringbuilder Str =   New Stringbuilder ();
Str. append ( " A " );
Edituser (user, STR, Code );

Console. writeline (CODE );
Console. writeline (STR );
Console. writeline (user. Name );
Console. writeline (user. Age );
Console. Readline ();

}

Public   Static   Void Edituser (User user, stringbuilder STR, String Code)
{
Code =   " VB. NET " ;
Str = Str. Remove ( 0 , 1 );
Str. append ( " E " );
User. Name =   " Lee " ;
User. Age =   " 10 " ;
}
}

Public   Class User
{
Private   String _ Name;
Private   String _ Age;
Public User ( String Name, String Age)
{
_ Name = Name;
_ Age = Age;
}

Public   String Name
{
Get { Return _ Name ;}
Set {_ Name = Value ;}
}
Public   String Age
{
Get { Return _ Age ;}
Set {_ Age = Value ;}
}
}

}

AboveCodeEnter the following information:

From the above we can see that the value of the string type has not changed, and the value of stringbuilder and class has changed.

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.