Wednesday, March 28, 2012

string.lenght vs string.empty

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 wrote:
> 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
On Wed, 27 Apr 2005 22:46:02 -0700 in
microsoft.public.dotnet.framework.aspnet, AVL
<AVL@.discussions.microsoft.com> wrote:

>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
"AVL" <AVL@.discussions.microsoft.com> a crit dans le message de
news:1F293E96-5693-4438-A520-B18BB5FF356C@.microsoft.com...
> 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?
It is true that checking the String.Length is faster.
Numeric functions are almost always more efficient than string functions.
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"AVL" <AVL@.discussions.microsoft.com> wrote in message
news:1F293E96-5693-4438-A520-B18BB5FF356C@.microsoft.com...
> 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?

0 comments:

Post a Comment