The 3 ways are:
String A= "";
1.if (a== "")
2.if (A==string.empty)
3.if (a.length==0)
3 methods are equivalent, so which one is the highest performance? I use the experiment to explain the problem.
Create 3 aspx pages (why use Web pages, mainly with Microsoft application Center Test)
WebForm1.aspx
private void Page_Load (object sender, System.EventArgs e)
{
String A= "";
for (int i=0;i<=1000000;i++)
{
if (a== "")
{
}
}
}
Webform2.aspx
private void Page_Load (object sender, System.EventArgs e)
{
String A= "";
for (int i=0;i<=1000000;i++)
{
if (a==string.empty)
{
}
}
}
Webform3.aspx
private void Page_Load (object sender, System.EventArgs e)
{
String A= "";
for (int i=0;i<=1000000;i++)
{
if (a.length==0)
{
}
}
}
Set up 3 stress test projects under Microsoft Application Center test:
Test results:
WebForm1.aspx----------if(a=="")
WebForm2.aspx-------if(a==String.Empty)
WebForm3.aspx-------if(a.Length==0)
So 3 ways to quantify the result is 98,105,168:
Method results
if (a== "") 98
if (A==string.empty) 105
if (a.length==0) 168
So why is if (a.length==0) the fastest?
Because integer judgments are equal to the fastest, there is no complicated process, such as instantiation.
So: We recommend that you determine whether the string is empty if (a.length==0).