By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,395 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

string.lenght vs string.empty

P: n/a
AVL
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?
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
Nov 19 '05 #2

P: n/a
On Wed, 27 Apr 2005 22:46:02 -0700 in
microsoft.public.dotnet.framework.aspnet, AVL
<AV*@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

Nov 19 '05 #3

P: n/a
Under normal circumstances it's unlikely to give a perceptible difference...

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

Patrice

--

"AVL" <AV*@discussions.microsoft.com> a écrit dans le message de
news:1F**********************************@microsof t.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?

Nov 19 '05 #4

P: n/a
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" <AV*@discussions.microsoft.com> wrote in message
news:1F**********************************@microsof t.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?

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.