C # Basics (interview often asked)

Source: Internet
Author: User

1. String StringsWhy is string a reference type?

Because string is a direct derivation of System.Object, it is a reference type.

why doesn't the string use new?

In fact, it can be new, generally we do not do so. First look at string a= "123"; String b= "123"; string s =new string (' 1 '); string t =new string (' 1 ');

Here A and b actually refer to the address of the same string, and the new one here is to reassign the address, so s and T are different reference addresses;

The CLR says that C # does not allow the use of the new operator to construct string objects from literal values;

If you use unsafe code, you can construct a string from char* or sbyte*, and then use new. string s =new string (' 1 '); string t =new string (' 1 ');

What is the difference between string and StringBuilder?

String strings are immutable, so it returns a new string when the string is manipulated. If a large number of string operations, a large number of string objects are created on the heap, resulting in more frequent garbage collection, which can affect performance;

The StringBuilder represents a mutable string.

C # Basics (interview often asked)

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.