string in C #. The difference between Empty, "" and null

Source: Internet
Author: User
Tags first string
This is a common problem, and there are already many discussions on the subject online. But I think it's all superficial understanding, and some people are misleading. I would like to say below my understanding of these three, if there is wrong place please correct me.

One: "" and string. Empty I think it's the same. There is an article on the web that has been reprinted dozens of times to say string. Empty does not allocate storage space, "" allocate a length of empty storage space, I think this sentence is wrong and ambiguous.

1, actually empty is a static read-only field in the String class, and his definition is this:

public static readonly String Empty = "";

That means string. The internal implementation of the empty is equal to "".

2, I would like to refute string. Empty does not allocate storage space, "" to allocate a length of empty storage space this view. First string. Empty and "" will be allocated storage space, specifically will be in the memory stack and heap on the allocation of storage space.

One thing to say is that the reference type is to save the object on the heap with the actual data, and to save the object on the stack on the heap. So string. Empty and "" will save an address on the stack. This address occupies 4 bytes, pointing to a space of 0 in the memory heap, which holds a string. The actual value of the empty. This I can use VS2010 tracking memory for everyone to demonstrate.

The 0x01e81228 in the figure above is the address that variable str stores in the stack.

For "", please see the figure below

The effect of this diagram is the same as the previous one, which means that "" also saves an address on the stack.

3. The CLR optimizes strings, so "" and string. Empty will also be optimized.

Declare the following two variables

String str1= "";

String Str2= "";

The str1 and str2 references will be the same, that is str1 the same address as str2 on the stack. Take a look at the picture below

It is obvious that the address str1 to STR2 is the same, which means that str1 occupies a space of 0 on the heap, and str2 does not open up new space on the heap, str2 to share the same space on the heap.

Similarly for

String str3=string. Empty;

String str4= string. Empty;

The address of STR3 and STR4 is the same. It also shares the same space on the memory heap. The following figure

The image above is the address of the STR3. The image above is the address of the STR4.

4, if you have to say "" and string. Empty what's different, I think 1 is written differently, string. Empty looks good ~!~. 2 is slightly different in terms of optimization. String. Empty in C # for "" At the syntax level optimization. This can be done by string. Empty the inner realization of the look out.

public static readonly String Empty = "";

That is, "" is optimized through the CLR, and the CLR maintains a string pool to prevent duplicate strings from being created in the heap. and string.      Empty is a C # syntax-level optimization that is optimized when the C # compiler compiles code to IL (that is, MSIL), that is, all access to the static field empty of the string class is directed to the same reference to conserve memory space. So the optimization of "" is more dependent on the CLR.

Let's take a look at the compiled IL Code:

String str1= ""; Compiled as follows:

ldstr ""/"" "" "from the string pool (actually take the address)

STFLD string CLASSLIBRARY1.CLASS1::STR1//Assign "" to str1 (actually assigned address)

String str2=string. Empty; Compiled as follows:

LDSFLD string [mscorlib]system.string::empty//gets the static field of the String class Empty (actually takes the address)

STFLD string classlibrary1.class1::str2//assigns empty to STR2 (actually assigns the address)

Summary: Said such a big push I feel wordy, and beginners friends may not understand. I am not good in philology, the ability to express the general also please forgive me, I will pick the key to say.

"" and string. There is no difference in usage and performance between empty. String. Empty is the optimization of "" at the syntactic level.

Two, String. The difference between empty and null

Because string. Empty is basically the same as "", so string. The difference between empty and Null also represents the difference between "" and null.

1, that's string. Empty will occupy a space of 0 on the heap, and Null will not. The specific contents are as follows:

String str1= "";

String Str2=null;

As just said STR1 will save an address on the stack, this address occupies 4 bytes, pointing to a space in the memory heap of 0, which holds the actual value of STR1.

STR2 also saves an address on the stack, which also accounts for 4 bytes, but this address is not explicitly pointed, it does not refer to, its content is 0x00000000. The following figure




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.