string.lenght vs string.empty

  • Thread starter Thread starter Guest
  • Start date Start date
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?
 
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
 
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
 
Under normal circumstances it's unlikely to give a perceptible difference...

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

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