String. Empty and "", the difference between null, with which better?

Source: Internet
Author: User
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 = =

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.