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!