V
Vicky
What is the better way of checking blank string is it
strvar.lenght > 0
or
strvar == ""
or something else
Thnaks
strvar.lenght > 0
or
strvar == ""
or something else
Thnaks
Vicky said:What is the better way of checking blank string is it
strvar.lenght > 0
or
strvar == ""
or something else
Vicky said:What is the better way of checking blank string is it
strvar.lenght > 0
or
strvar == ""
or something else
Thnaks
Vicky said:What is the better way of checking blank string is it
strvar.lenght > 0
or
strvar == ""
or something else
Julie said:I use:
strvar == string.Empty
for no particular reason.
Personally, I always use:
if (String.Empty.Equals(strvar))
{
...
}
Ed Courtenay said:Julie said:I use:
strvar == string.Empty
for no particular reason.
Personally, I always use:
if (String.Empty.Equals(strvar))
{
...
}
--
Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
Jared Parsons said:It's better performance wise to check if the Length is 0
--
Jared Parson [MSFT]
(e-mail address removed)
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
message news:[email protected]...Julie said:Vicky wrote:
What is the better way of checking blank string is it
strvar.lenght > 0
or
strvar == ""
or something else
Thnaks
I use:
strvar == string.Empty
for no particular reason.
Personally, I always use:
if (String.Empty.Equals(strvar))
{
...
}
--
Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
cody said:I think the jitter will always optimize s == "" to s.Length == 0
anyway.
I don't think so - for a start, it would have to add in a nullity test.
It's not terribly easy to pick out from a disassembly of optimised
native code, but I'm pretty sure it's *not* making that optimisation.
The following test program seems to bear that out. Note how many
iterations are needed to see a really significant difference though...
using System;
class Test
{
const int Iterations = 1000000000;
static void Main()
{
{
DateTime start = DateTime.Now;
int x = 0;
for (int i=0; i < Iterations; i++)
{
x += Bar("");
}
DateTime end = DateTime.Now;
Console.WriteLine ("{0} {1}", x, end-start);
}
{
DateTime start = DateTime.Now;
int x = 0;
for (int i=0; i < Iterations; i++)
{
x += Foo("");
}
DateTime end = DateTime.Now;
Console.WriteLine ("{0} {1}", x, end-start);
}
}
static int Foo (string x)
{
if (x=="")
{
return 1;
}
return 0;
}
static int Bar (string x)
{
if (x.Length==0)
{
return 1;
}
return 0;
}
}
cody said:operator== internally calls string.Equals which certainly will return true
if (s1.Lenght==0l && s.Length==0) first, so your test has very little value
since you do a method call too.
But if the jitter would optimize s=="" to s!=null && s.Lenght==0 we can
certainly expect a noticable performance improvement.
Jon Skeet said:Why does it have very little value? It shows that
if (s=="")
isn't as fast a test as
if (s.Length==0)
which was what was being tested. Don't forget that .Length is
effectively a method call too, as Length is a property.
*If* it did, yes - but there's no evidence that it *does* do that
optimisation.