asp.net String.Empty NULL Difference _ Practical Tips

Source: Internet
Author: User
This article makes a rough explanation of the difference between them.
string. Empty is the equivalent of "
Typically used for initialization of strings
Like what:
String A;
Console.WriteLine (a);//There will be an error, because there is no initialization of a
And there is no error:
String a=string. Empty;
Console.WriteLine (a);
or For comparison:
if (a== "")
if (a==string. Empty)
The above two sentences are the same effect.
String. Empty does not allocate storage space
"" Allocates a storage space with an empty length
So you usually use string. Empty
In order to cross the platform later, or with String.Empty
In C #, most of the cases "" and string. Empty can be used interchangeably. Like what:
Copy Code code as follows:

string s = "";
String s2 = string. Empty;
if (s = = string. Empty) {
//
}

If statement set up
String.Empty and NULL, both representing an empty string, string str1= String.Empty, after this definition, the str1 is an empty string, the empty string is a special string, except that the value of the string is null, there is an accurate point in memory, string str2=null, so defined, just define a reference to a string class , STR2 does not point to any place, if not instantiated before use, will be an error. The TextBox1.Text value is a zero-length string "".
Several ways to determine an empty string are in the order of performance from highest to lowest:
S.length = 0 is superior to s = = string. Empty is superior to S = = ""
The best way to judge whether a string is empty 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.