What I understand is the difference between string S = string. Empty, string S = NULL and string S = ""

Source: Internet
Author: User
For those who do not pay much attention to. net, it may be difficult to know the detailed differences between the three.
The first time I encountered this problem was during an interview, let me talk about my ignorance:
First, let's talk about the difference between string S = NULL and string S =. Literally, both initialize variables with null values. The former can be associated with objects, so it can be said that "Initialization is a null reference"; the latter can be said that "Initialization is a null value ". It seems that there is indeed an "unspeakable secret" between the two. Of course, this difference is only our understanding of superficial perceptual knowledge. Theoretically, string S = NULL does not allocate storage space to it, while string S = "" assigns it a storage space with an empty length.
In my opinion, since we understand from a perceptual perspective that string S = "" is "null" and the string type is also a reference type, it should certainly be inHeapThe previous Slot" , Which contains a null value, while the stack saves a pointer to the heap. String S = NULL is different. Since it is considered as "null reference", it willStackThe previous Slot" , There is a pointer in it that does not point to any place (in other words, there is no value on the stack ).
next, let's talk about the difference between string S = string. Empty and string S = NULL. Here we also want to talk about the difference between string and string. We can't see the difference between them during development in Visual Studio, because the information presented to us through intelligent sensing is "representing text, a series of Unicode characters ". Haha! We know that in. all reference types and value types in. NET are inherited from a root type object, and the Environment for survival of these "things" is CTS, because they are defined in CTS, CTS also depends on CLR, so there is no CTS without CLR. To put it bluntly, my ignorance is that string is the eldest brother, because it is defined in CTS and is based on CLR. String is only a variant of string in the C # syntax. It is based on C. There seems to be no difference between the two. It's just a specific symbol in different environments. I don't know if this is correct? Haha
okay, String. empty and null are like this. Both of them indicate null. The former is an empty string, but the value of this string is empty, but there is an accurate point in the memory stack; the latter only defines a reference of the string type, and the variable does not point to any place, or the sentence it occupies the stack as" pitfall" , A pointer does not point to any place. if it is not instantiated before use, an error is returned.
finally, we can also find three methods to indicate that the string is null, S. length = 0, S = string. empty, S = "", the performance of the three also gradually decreases.
I hope you can point out any errors in time. Thank you!

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.