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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • j_ruez

    #1

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

    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
  • Tobin Harris

    #2
    Re: .text == "" vs .length==0

    "j_ruez" <j_ruez@yahoo.c om> wrote in message
    news:17b6d740.0 310020656.3e765 569@posting.goo gle.com...[color=blue]
    > 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[/color]

    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


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: .text == &quot;&quot; vs .length==0

      j_ruez,

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

      if (pstrString == null || pstrString.Leng th == 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.Emp ty" for as well).

      Hope this helps.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - nick(dot)paldin o=at=exisconsul ting<dot>com

      "j_ruez" <j_ruez@yahoo.c om> wrote in message
      news:17b6d740.0 310020656.3e765 569@posting.goo gle.com...[color=blue]
      > 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[/color]


      Comment

      • Mickey Williams

        #4
        Re: .text == &quot;&quot; vs .length==0

        The most performant way is to check for Length == 0.

        --
        Mickey Williams
        Author, "Microsoft Visual C# .NET Core Reference", MS Press



        "Tobin Harris" <comedyharris@h otmail.com> wrote in message
        news:blhelg$cf8 ce$1@ID-135366.news.uni-berlin.de...[color=blue]
        > "j_ruez" <j_ruez@yahoo.c om> wrote in message
        > news:17b6d740.0 310020656.3e765 569@posting.goo gle.com...[color=green]
        > > 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[/color]
        >
        > 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
        >
        >[/color]


        Comment

        • Jon Skeet

          #5
          Re: .text == &quot;&quot; vs .length==0

          Tobin Harris <comedyharris@h otmail.com> wrote:[color=blue]
          > A more descriptive, and potentially platform-safe way could be this:
          >
          > txtValue.Text == string.Empty[/color]

          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.
          [color=blue]
          > However, bear in mind that strings can also be null, so you may want to
          > check this also![/color]

          That's very true.

          --
          Jon Skeet - <skeet@pobox.co m>
          Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

          If replying to the group, please do not mail me too

          Comment

          • Chris R. Timmons

            #6
            Re: .text == &quot;&quot; vs .length==0

            j_ruez@yahoo.co m (j_ruez) wrote in
            news:17b6d740.0 310020656.3e765 569@posting.goo gle.com:
            [color=blue]
            > 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.[/color]

            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().Lengt h == 0);
            }
            }


            Hope this helps.

            Chris.
            -------------
            C.R. Timmons Consulting, Inc.

            Comment

            • Jeff

              #7
              Re: .text == &quot;&quot; vs .length==0

              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_ruez@yahoo.c om> wrote in message
              news:17b6d740.0 310020656.3e765 569@posting.goo gle.com...[color=blue]
              > 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[/color]


              Comment

              • Jon Skeet

                #8
                Re: .text == &quot;&quot; vs .length==0

                Jeff <jeff@nowhere.c om> wrote:[color=blue]
                > 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 "").[/color]

                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 - <skeet@pobox.co m>
                Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

                If replying to the group, please do not mail me too

                Comment

                • Jeff

                  #9
                  Re: .text == &quot;&quot; vs .length==0

                  Jon,

                  Thanks for the clarification.

                  --- Jeff

                  "Jon Skeet" <skeet@pobox.co m> wrote in message
                  news:MPG.19e670 e8b5e4674d9897b f@msnews.micros oft.com...[color=blue]
                  > Jeff <jeff@nowhere.c om> wrote:[color=green]
                  > > Although I haven't verified this, my assumption would be that if you[/color][/color]
                  used[color=blue][color=green]
                  > > 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 "").[/color]
                  >
                  > 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 - <skeet@pobox.co m>
                  > http://www.pobox.com/~skeet
                  > If replying to the group, please do not mail me too[/color]


                  Comment

                  • mikeb

                    #10
                    Re: .text == &quot;&quot; vs .length==0

                    j_ruez wrote:[color=blue]
                    > 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[/color]

                    Let's look at the IL:

                    (txtValue.Text == "") boils down to a call to String.op_Equal ity(),
                    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.Lengt h == 0) boils down to a call to
                    String.get_Leng th(), 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

                    Comment

                    • j_ruez

                      #11
                      Re: .text == &quot;&quot; vs .length==0

                      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

                      Comment

                      • Michael Earls

                        #12
                        Re: .text == &quot;&quot; vs .length==0

                        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" <mailbox.google @mailnull.com> wrote in message
                        news:OFHhbeRiDH A.1368@TK2MSFTN GP12.phx.gbl...[color=blue]
                        > j_ruez wrote:[color=green]
                        > > 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[/color]
                        >
                        > Let's look at the IL:
                        >
                        > (txtValue.Text == "") boils down to a call to String.op_Equal ity(),
                        > 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.Lengt h == 0) boils down to a call to
                        > String.get_Leng th(), 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
                        >[/color]


                        Comment

                        • Daniel O'Connell

                          #13
                          Re: .text == &quot;&quot; vs .length==0


                          "Michael Earls" <mearls@hotmail .com> wrote in message
                          news:%23Ni3xqgi DHA.1952@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                          > I don't have an answer, only a preference. I use String.Empty because[/color]
                          there[color=blue]
                          > 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[/color]
                          "safer"[color=blue]
                          > using String.Empty because it is a static method of the String class.
                          >
                          > Just curious.
                          >[/color]

                          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.
                          [color=blue]
                          > Michael Earls
                          >
                          > "mikeb" <mailbox.google @mailnull.com> wrote in message
                          > news:OFHhbeRiDH A.1368@TK2MSFTN GP12.phx.gbl...[color=green]
                          > > j_ruez wrote:[color=darkred]
                          > > > 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[/color]
                          > >
                          > > Let's look at the IL:
                          > >
                          > > (txtValue.Text == "") boils down to a call to String.op_Equal ity(),
                          > > 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.Lengt h == 0) boils down to a call to
                          > > String.get_Leng th(), 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
                          > >[/color]
                          >
                          >[/color]


                          Comment

                          • Daniel O'Connell

                            #14
                            Re: .text == &quot;&quot; vs .length==0


                            "Michael Earls" <mearls@hotmail .com> wrote in message
                            news:%23Ni3xqgi DHA.1952@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                            > I don't have an answer, only a preference. I use String.Empty because[/color]
                            there[color=blue]
                            > 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[/color]
                            "safer"[color=blue]
                            > using String.Empty because it is a static method of the String class.
                            >
                            > Just curious.
                            >[/color]

                            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.
                            [color=blue]
                            > Michael Earls
                            >
                            > "mikeb" <mailbox.google @mailnull.com> wrote in message
                            > news:OFHhbeRiDH A.1368@TK2MSFTN GP12.phx.gbl...[color=green]
                            > > j_ruez wrote:[color=darkred]
                            > > > 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[/color]
                            > >
                            > > Let's look at the IL:
                            > >
                            > > (txtValue.Text == "") boils down to a call to String.op_Equal ity(),
                            > > 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.Lengt h == 0) boils down to a call to
                            > > String.get_Leng th(), 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
                            > >[/color]
                            >
                            >[/color]


                            Comment

                            Working...