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 13 1899
"j_ruez" <j_****@yahoo.com> wrote in message
news:17**************************@posting.google.c om... 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.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com
"j_ruez" <j_****@yahoo.com> wrote in message
news:17**************************@posting.google.c om... 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
The most performant way is to check for Length == 0.
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press www.servergeek.com
"Tobin Harris" <co**********@hotmail.com> wrote in message
news:bl************@ID-135366.news.uni-berlin.de... "j_ruez" <j_****@yahoo.com> wrote in message news:17**************************@posting.google.c om... 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
Tobin Harris <co**********@hotmail.com> wrote: 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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too j_****@yahoo.com (j_ruez) wrote in
news:17**************************@posting.google.c om: 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.
-------------
C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
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
"j_ruez" <j_****@yahoo.com> wrote in message
news:17**************************@posting.google.c om... 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
Jeff <je**@nowhere.com> wrote: 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 Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon,
Thanks for the clarification.
--- Jeff
"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Jeff <je**@nowhere.com> wrote: 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 Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
j_ruez wrote: 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 "".
--
mikeb
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
"mikeb" <ma************@mailnull.com> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl... j_ruez wrote: 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 "".
-- mikeb
"Michael Earls" <me****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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
"mikeb" <ma************@mailnull.com> wrote in message news:OF**************@TK2MSFTNGP12.phx.gbl... j_ruez wrote: 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 "".
-- mikeb
"Michael Earls" <me****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... 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
"mikeb" <ma************@mailnull.com> wrote in message news:OF**************@TK2MSFTNGP12.phx.gbl... j_ruez wrote: 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 "".
-- mikeb
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jukka K. Korpela |
last post by:
No, I'm not making this up, this is what http://www.w3.org/ says when
accessed with a text browser, or in any decent browser in no-images
mode:
"Tim Berners-Lee smiling 2003-12-31: Buckingham...
|
by: moondaddy |
last post by:
Is there a asp.net validator control that validates the length of the text
being entered or does everyone just write jscript for this?
--
moondaddy@nospam.com
|
by: Kivak Wolf |
last post by:
Hey everyone,
I have a textbox in my web page that is going to be used to enter an
E-mail into (just plain text, no HTML). Now, this will interact with a
SQL database where the contents of the...
|
by: Linda |
last post by:
Hi,
Is there a way to do a "text" (rather than "binary") compareison with
the "like" operator, without changing the global "Option Compare"
setting? I don't want to risk breaking many, many...
|
by: gabon |
last post by:
I'm creating a select entirely through JavaScript and very strangely IE
doesn't show the text in the option elements.
Here part of the code:
this.form_country=document.createElement("select");...
|
by: bbcdancer |
last post by:
Is it possible to restrict the length of a text box in MS Access using
VBA on condition to what is selected in a list combo box.
Scenario:
1. I have a list combo box containing:
AA
BBB...
|
by: Arielle |
last post by:
I'm trying to save a memo to a database..
Right now it's declared as "text"
what's the max size of that?
|
by: mervyn |
last post by:
Hi There,
I am very new to .NET and VB so this may be a really simple thing to solve but here is my issue
I have a a textbox with a value in it. I know what the length of the text in the text...
|
by: blad3runn69 |
last post by:
hi, just wondering how you good peoples get around combobox width and text length, in the context of making it easy for the user to read a long string?
like this example
Thanks for your help it...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |