.text == "" vs .length==0

  • Thread starter Thread starter j_ruez
  • Start date Start date
J

j_ruez

I am trying to learn the best ways to do some things in c#

When testing for an empty string, what is the best way to test it.
For example, if I have a textbox named txtValue

Should I use

txtValue.Text == ""

or

txtValue.Length == 0

and why would I use one over the other. And if it doesn't matter,
just let me know that also.

Thanks
 
j_ruez said:
When testing for an empty string, what is the best way to test it.
For example, if I have a textbox named txtValue

Should I use

txtValue.Text == ""

or

txtValue.Length == 0

A more descriptive, and potentially platform-safe way could be this:

txtValue.Text == string.Empty

However, bear in mind that strings can also be null, so you may want to
check this also!

Tobin
 
j_ruez,

I personally would use the following when working with strings in
general:

if (pstrString == null || pstrString.Length == 0)
// Perform operation here.

The reason for this is that you are not guaranteed to always hold a
reference to the string. Now your logic for what to do with a null
reference might be different than others, but you should take it into
consideration.

Also, the TextBox class might always return an empty string, in which
case, I would just check the length (it should be quicker than checking
against the empty string, which you should use "String.Empty" for as well).

Hope this helps.
 
Tobin Harris said:
A more descriptive, and potentially platform-safe way could be this:

txtValue.Text == string.Empty

I don't see how it would ever be more platform-safe. As for descriptive
- I think on this one it's a matter of preference. I'd probably use
=="" myself, but that's not to say that using String.Empty isn't just
as good.
However, bear in mind that strings can also be null, so you may want to
check this also!

That's very true.
 
(e-mail address removed) (j_ruez) wrote in
I am trying to learn the best ways to do some things in c#

When testing for an empty string, what is the best way to test it.
For example, if I have a textbox named txtValue

Should I use

txtValue.Text == ""

or

txtValue.Length == 0

and why would I use one over the other. And if it doesn't matter,
just let me know that also.

Both ways are OK. To build on Tobin's answer, you can use a small
utility method to make testing for an empty or null string easier:

public sealed class StringUtilities
{
public static bool IsStringEmpty(
string s)
{
return (s == null) || (s.Length == 0);
}
}

You may also consider a string that contains only whitespace to be
"empty". In that case, the Trim() method can be used to strip
leading and trailing whitespace from the string before the Length
test is done:

public sealed class StringUtilities
{
public static bool IsStringEmpty(
string s)
{
return (s == null) || (s.Trim().Length == 0);
}
}


Hope this helps.

Chris.
 
Although I haven't verified this, my assumption would be that if you used
the - txtValue.Text == "" method to test if a string was empty, then an
extra string object would have to be created at run-time (for the "").
Although this probably isn't that big of an issue, I suppose it could be if
it were implemented within a large loop.

Using the - txtValue.Length == 0 way shouldn't have to create the extra
string object.

Just my $0.02 worth.

--- Jeff
 
Jeff said:
Although I haven't verified this, my assumption would be that if you used
the - txtValue.Text == "" method to test if a string was empty, then an
extra string object would have to be created at run-time (for the "").

No. String literals are interned when classes are loaded. In other
words, "" is a reference to the same string wherever it's used.
 
Jon,

Thanks for the clarification.

--- Jeff

Jon Skeet said:
No. String literals are interned when classes are loaded. In other
words, "" is a reference to the same string wherever it's used.
 
j_ruez said:
I am trying to learn the best ways to do some things in c#

When testing for an empty string, what is the best way to test it.
For example, if I have a textbox named txtValue

Should I use

txtValue.Text == ""

or

txtValue.Length == 0

and why would I use one over the other. And if it doesn't matter,
just let me know that also.

Thanks

Let's look at the IL:

(txtValue.Text == "") boils down to a call to String.op_Equality(),
which in turn calls String.Equals( string, string). This checks for
reference equality and does some checks for null parameters before
calling String.Equals( string), which is implemented in native code, not
IL, and ILDASM won't tell me what's in there. I decided I didn't need to
go any further (farther?). I'll assume that the first thing
String.Equals does is check for the lengths being the same; actually it
probably checks for reference equality and/or a null parameter first.

(txtValue.Length == 0) boils down to a call to
String.get_Length(), followed by a test for 0 (generally a 'brtrue' IL
instruction).

So, the second idiom is certainly more efficient in terms of IL - but,
we're looking at IL which may well be JIT'ed to some very efficient code
(in both cases), since the String class is sealed.

My opinion (for what it's worth) is to use whichever idiom seems most
readable to you, which may well be different in different areas of code.
I think the real-world speed difference will never be noticed (or even
measurable) in code that's doing any real work with strings.

Also, I'd suggest not using String.Empty (which I've seen in other
responses) - it buys you nothing, and I find it to be far less readable
than the simple, natural, well-used "".
 
just wanted to say thanks for all the follow up messages. You have
all helped me answer my question and I appreciate you taking the time
to do so.

--jarrod
 
I don't have an answer, only a preference. I use String.Empty because there
is this mysterious issue of localization and right-to-left strings out
there. Like I said, I don't know for sure, but does "" mean something
entirely different in Kanji, Arabic, or Simplified Chinese? I feel "safer"
using String.Empty because it is a static method of the String class.

Just curious.

Michael Earls
 
Michael Earls said:
I don't have an answer, only a preference. I use String.Empty because there
is this mysterious issue of localization and right-to-left strings out
there. Like I said, I don't know for sure, but does "" mean something
entirely different in Kanji, Arabic, or Simplified Chinese? I feel "safer"
using String.Empty because it is a static method of the String class.

Just curious.

I don't know for sure either, I can tell you it is fine as far as
Kanji\Katakana\Hiragana, but as far as other languages, I don't know.
However, the standard string .ctor doesn't look anything up, it simply does
this.Empty = ""; I sincerly hope they aren't changing it manually for each
language, and that they aren't changing it in each other .ctor, cause thats
kinda silly.
The docs also say, currently anyway, The value of this field is the
zero-length string, "".

Really, "" is a string class with length 0, how can that ever mean anything
else? I've yet to see a really good explination for why the field even
exists.
Michael Earls
 
Back
Top