Asp.net string. Empty null difference

Source: Internet
Author: User

This article gives a rough description of their differences.
String. Empty is equivalent ""
It is generally used for string initialization.
For example:
String;
Console. writeline (a); // an error is reported because a is not initialized.
No error is reported below:
String A = string. empty;
Console. writeline ();
Or for comparison:
If (A = "")
If (A = string. Empty)
The above two statements have the same effect.
String. Empty does not allocate storage space
"" Allocate a bucket with an empty Length
Therefore, we generally use string. Empty
For the sake of future cross-platform use string. Empty
In C #, "" And string. Empty Can be used interchangeably. For example: CopyCode The Code is as follows: String S = "";
String S2 = string. empty;
If (S = string. Empty ){
//
}

If statement is true
String. empty and null, both of which indicate null strings, string str1 = string. empty. After this definition, str1 is an empty string, and the empty string is a special string, except that the value of this string is null and has an accurate point in 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!

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.