What are the differences and connections between string. Empty, null, and "" In Asp.net (C? [Discussion Question]

Source: Internet
Author: User

First, let's take a look at the following:CodeWhat do you think are the results?

 
StringSTR =String. Empty;StringStr1 ="";StringStr2 =Null;BoolT =String. Isnullorempty (STR );BoolT1 =String. Isnullorempty (str1 );BoolT2 =String. Isnullorempty (str2 );

The output result is displayed. All are true. Is it depressing? Why is it true. Is there no difference?

The three assignment methods. I must have written all the code. After all, I learned the following online. Share this

On the network: String. Empty is equivalent "",

But here we are talking about "equivalent", not "equal"

Obviously, the above values are all values, but if this is the case, an error will be reported. It also verifies that the value must be initialized before use, that is, the value assignment.

 
StringStr1;BoolT1 =String. Isnullorempty (str1); console. writeline (str1 );

Since the value assignment is equal. Where is the difference? Is memory allocation?

The most common interview question is: What is the difference between string STR = "" and string STR = NULL, which should be familiar to everyone. The answer is: the former allocates space with a Null String, that is, the length is null, but the latter does not allocate space at all. So is the latter cost-effective in terms of efficiency?

String STR = string. empty does not allocate memory space either. In the beginning, empty is equivalent to "", but now empty does not allocate memory space like null. How can it become a family.

There is another saying on the Network: to use string. Empty for cross-platform platforms in the future. Amount. "" And null are not cross-platform?

The following is an explanation on the network:

String. Empty and null, both of which indicate empty strings

String str1 = string. Empty. After this definition, str1 is an empty string, and the empty string is a special string,

However, the value of this string is null, Which is accurate to the memory.

String str2 = NULL. After this definition, a reference to the string class is defined. str2 does not point to any place. If it is not instantiated before use, an error is returned. Textbox1.text is a zero-length string "".

Several methods for determining as a Null String, in the order of performance from high to low:

S. Length = 0 is better than S = string. Empty is better than S = ""

The best way to judge whether the string is null is S. Length = 0!

 

Then I found an interesting experiment:

A foreigner tests five types of objects, which are more efficient:

S = ""

S = string. Empty

S. Equals ("")

S. Equals (string. Empty)

S. Length = 0

Test code see: http://www.hn1c.com/diannao/dn87571/

Test results:

The test result is as follows:

 

[S = ""]

Empty string, 10315.6250 Ms short string, 8307.8125 ms long string, 8564.0625 Ms

[S = string. Empty]

Empty string, 3573.4375 Ms short string, 8307.8125 ms long string, 8603.1250 Ms

[S. Equals ("")]

Empty string, 9517.1875 Ms short string, 7537.5000 ms long string, 7576.5625 Ms

[S. Equals (string. Empty)]

Empty string, 9540.6250 Ms short string, 7515.6250 ms long string, 7607.8125 Ms

[S. Length = 0]

Empty string, 443.7500 Ms short string, 443.7500 ms long string, 445.3125 Ms

Obviously, the Length attribute of a string is the fastest.

The following conclusions are obtained:
Use S. equals ("stringtocompare") to determine whether non-null strings are equal. Use S. length = 0 to determine whether the string is a null string (note that this cannot be used to determine whether the string is null, otherwise the error "the object reference is not set to the instance of the object" will occur ).

Use string. isnullorempty (STR) to judge whether the string is null in 2.0 );

---------------------------------------------- After reading the comments, I continue to mine -------------------------------------------

If the preceding information is not clear, check the internal definition of empty: public static readonly string empty, a static read-only field.

We might as well test their memory allocated space by ourselves

We know that stirng is a reference type. The reference type is to save the actual data of an object on the stack, save the address of the object on the stack, that is, the address on the stack points to the value in the stack. Or the reference type is an address that points to a value.

Open vs2012 and test the space opened on the stack.

We will also perform three tests to see the situation where empty, null, and "" opened up space on the stack respectively.

Figure 1:

 

Figure 2:

 

Figure 3:

From figure 1 and figure 2, we can see that there are addresses on the empty and "" stacks, which indicates that there are pointing values in the heap, which means that both empty and "" will open up space.

However, figure 3 also has an address, but there is no point.

The passing experts came in to discuss the differences between the three and the friendly and positive speeches. Are they just as explained above. Thank you!

 

Related Article

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.