better way of checking for blank string

  • Thread starter Thread starter Vicky
  • Start date Start date
V

Vicky

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

Either of those will work (typos aside - the property is Length, and
you should be checking for it being 0, not being greater than 0). You
may find the latter more readable. The former is likely to be slightly
more efficient, but frankly it's not going to amount to a great
difference in most cases.
 
Vicky said:
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.
 
What is the better way of checking blank string is it
Personally, I always use:

if (String.Empty.Equals(strvar))
{
...
}


*lol*

Why not simply

if (System.String.Compare(System.String.Empty, strval, false) == 0)
{
....
}
 
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

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
 
I think the jitter will always optimize s == "" to s.Length == 0 anyway.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
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;
}
}
 
I think the jitter will always optimize s == "" to s.Length == 0
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;
}
}


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.
 
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.

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.
But if the jitter would optimize s=="" to s!=null && s.Lenght==0 we can
certainly expect a noticable performance improvement.

*If* it did, yes - but there's no evidence that it *does* do that
optimisation.
 
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.

It would seem to me that s.Length is readily available directly (as it is
stored *somewhere*), whereas a string comparison would need more processing
(via some internal algorithm) even in the trivial "" condition. Besides, MS
may not have optimized s=="" since s.Length==0 is available.
 
Back
Top