string.lenght vs string.empty

G

Guest

Hi,
I read that to check for empty strings , comparing strings using
String.Length == 0 is faster than String.Empty or "" . Is it true...?
If so, how?
 
H

Hans Kesting

AVL said:
Hi,
I read that to check for empty strings , comparing strings using
String.Length == 0 is faster than String.Empty or "" . Is it true...?
If so, how?

I think it is faster because:
* Length is stored, doesn't need to be calculated
* integer comparison (Length == 0) is easier that string comparison,
which (I think) needs to be done on a character-by-character basis.

Hans Kesting
 
R

Roger Helliwell

Hi,
I read that to check for empty strings , comparing strings using
String.Length == 0 is faster than String.Empty or "" . Is it true...?
If so, how?

I think str.Length is probably faster, however if your string is null,
using Length will throw a NRE. I prefer to use
string.IsNullOrEmpty(someString). It safely works on null strings as
well as empty strings.

Roger
 
P

Patrice

Under normal circumstances it's unlikely to give a perceptible difference...

You could easily measure and see what the IL is...

Patrice
 
S

Steve C. Orr [MVP, MCSD]

It is true that checking the String.Length is faster.
Numeric functions are almost always more efficient than string functions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top