Null is a C # keyword and is a literal value that represents a null reference that does not reference any object. NULL is the default value for reference to a type variable. Then only the reference variable can be null, and if int i=null, it is not possible, because int is a value type.
String. Empty is the equivalent of "", but they and Null are two different things.
It is said to be: string. Empty does not allocate storage space
"" Allocates a null-length storage space, so string. Empty is more efficient than "". (High efficiency is right, but whether or not to allocate storage space, is not clear, pending verification)
A foreigner has done a test, the test object has 5 kinds, respectively: s = = "" "s = = string. Empty s.equals ("") s.equals (string. Empty) S.length = = 0
Pass the following test code (if you want to see the plus sign below), judge who is more efficient ~
Speed Code #region Speed code
const int cntries = 10;
const int cniterations = 1000000000;
DateTime Dtstart, Dtend;
TimeSpan ts;
Double femptyquotes = 0, fshortquotes = 0, flongquotes = 0;
Double femptyempty = 0, Fshortempty = 0, flongempty = 0;
Double femptydotquotes = 0, fshortdotquotes = 0, flongdotquotes = 0;
Double femptydotempty = 0, Fshortdotempty = 0, flongdotempty = 0;
Double femptydotlength = 0, fshortdotlength = 0, flongdotlength = 0;
int I, J;
String sempty = String. Empty;
String Sshort = "This is a short string to test empty string comparison";
String Slong = "This is a long string to test the efficiency of comparing with empty strings, which means it has to be lik E, really long. And I ' m starting to-run out of the useless things to say ... ";
for (j = 0; j < cntries; ++j)
... {
Double femptyquotes = 0, fshortquotes = 0, flongquotes = 0;
Dtstart = DateTime.Now;
for (i = 0; i < cniterations; ++i)
... {
if (Sempty = = "");
}
Dtend = DateTime.Now;
ts = Dtend-dtstart;
Femptyquotes + + TS. TotalMilliseconds;
Dtstart = DateTime.Now;
for (i = 0; i < cniterations; ++i)
... {
if (Sshort = = "");
}
Dtend = DateTime.Now;
ts = Dtend-dtstart;
Fshortquotes + + TS. TotalMilliseconds;
Dtstart = DateTime.Now;
for (i = 0; i < cniterations; ++i)
... {
if (Slong = = "");
}
Dtend = DateTime.Now;
ts = Dtend-dtstart;
Flongquotes + + TS. TotalMilliseconds;
Double femptyempty = 0, Fshortempty = 0, flongempty = 0;
Dtstart = DateTime.Now;
for (i = 0; i < cniterations; ++i)
... {
if (sempty = = string. Empty);
}
Dtend = DateTime.Now;
ts = Dtend-dtstart;
Femptyempty + + TS. TotalMilliseconds;
Dtstart = DateTime.Now;
for (i = 0; i < cniterations; ++i)
... {
if (Sshort = =