String is a reference type, but it also has some value type 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 ("");
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)
& N