C # BASICS (4)

Source: Internet
Author: User

1. Similarities and Differences between string and stringbuilder

String is an unchangeable data type. Once a String object is initialized, the string object cannot be changed. Method for modifying string content

And operator are actually creating a new string.

The stringbuilder allocates memory and modifies the string in the storage unit assigned to the stringbuilder instance.

Example:

The string cannot be modified:
String STR = "";
STR = "B" // STR is assigned "B". That's right, but the original string value "A" in STR is not modified.
String STR = "";
String str1 = STR;
STR + = "B"; // STR = "AB", str1 = "";

Stringbuilder STR = new stringbuilder ("");
Stringbuilder str1 = STR;
Str. append ("B ");
Str. tostring () and str1.tostring () are both "AB ".

2. Explanation string STR = NULL/string STR = ""

STR = NULL, declared regardless of memory space, STR = "" declared and divided into memory space

 

3. String S = new string ("XYZ"); how many string objects are created?

Two objects: xyx and xyx.

 

4. Can I inherit the string class?
The string class is a final class, so it cannot be inherited.

 

5. String S = "";AndString S = string. empty;What is the difference? which method is better?

String S = "";Allocate a block of memory for storage first""Text constant,SReference this memory,String S = string. Empty;SReferenceStringThe static variable address in the class (the static variable will be initialized at the beginning, so no matter how many times it is referenced, only one memory will be allocated). When comparing the two, the latter has one less memory allocation, therefore, it is more efficient.

 

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.