checking for empty string

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

    checking for empty string

    which one do you use and why?

    MyString == null || MyString == ""

    vs

    MyString == null || MyString.Length == 0


  • LEBRUN Thomas

    #2
    RE: checking for empty string

    Checking the length is better than checking if the string is empty or not :)

    Bye.

    -------------------
    LEBRUN Thomas




    "Dan Bass" wrote:
    [color=blue]
    > which one do you use and why?
    >
    > MyString == null || MyString == ""
    >
    > vs
    >
    > MyString == null || MyString.Length == 0
    >
    >
    >[/color]

    Comment

    • James Curran

      #3
      Re: checking for empty string

      I usually go with:[color=blue]
      > MyString == null || MyString.Length == 0[/color]

      Poking around a bit with Lutz Roeder's .Net Reflector, it seems that this
      version is the equivalent of[color=blue]
      > MyString == null || MyString.m_stri ngLength == 0[/color]

      while the other version if the equivalent of
      [color=blue]
      > MyString == null ||[/color]
      CultureInfo.Cur rentCulture.Com pareInfo.Compar e(MyString, new String (""),
      CompareOptions. None) == 0

      --
      Truth,
      James Curran
      [erstwhile VC++ MVP]
      Home: www.noveltheory.com Work: www.njtheater.com
      Blog: www.honestillusion.com Day Job: www.partsearch.com

      "Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
      news:%237iEkNlz EHA.3204@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > which one do you use and why?
      >
      > MyString == null || MyString == ""
      >
      > vs
      >
      > MyString == null || MyString.Length == 0
      >
      >[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: checking for empty string

        <"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk>> wrote:[color=blue]
        > which one do you use and why?
        >
        > MyString == null || MyString == ""
        >
        > vs
        >
        > MyString == null || MyString.Length == 0[/color]

        Well, the latter is slightly more efficient, so you should use that if
        this is a bottleneck in your application, but really it's a case of
        whichever you find most readable, in most cases. I personally prefer
        the first, but it's really a matter of taste.

        --
        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

        • Richard Blewett [DevelopMentor]

          #5
          Re: checking for empty string

          I use

          MyString == null || MyString == string.Empty

          as I think its the clearest with hard coded values

          Regards

          Richard Blewett - DevelopMentor



          which one do you use and why?

          MyString == null || MyString == ""

          vs

          MyString == null || MyString.Length == 0

          Comment

          • Richard Blewett [DevelopMentor]

            #6
            Re: checking for empty string

            should say "without hard coded values"

            Regards

            Richard Blewett - DevelopMentor



            nntp://news.microsoft. com/microsoft.publi c.dotnet.langua ges.csharp/<#IguMVlzEHA.28 76@TK2MSFTNGP12 .phx.gbl>

            I use

            MyString == null || MyString == string.Empty

            as I think its the clearest with hard coded values


            Comment

            • Bob Grommes

              #7
              Re: checking for empty string

              It's a trade off between a slight advantage in clarity vs a slight advantage
              in performance (actually MyString == "" has something more than a slight
              performance advantage in theory, but in practice it isn't that often that it
              amounts to so much of a difference that a user would actually notice).

              I personally use MyString.Length == 0 out of habit, as I think it's pretty
              self-explanatory.

              As an aside, String.Empty is completely equivalent to MyString == "",
              performance-wise -- String.Empty is simply a language-independent way to
              express it. It would also have the advantage of being utterly clear, except
              that in my experience many developers seem fuzzy on exactly what Empty
              really means. I mean, *I'm* clear on what it means yet when I first
              encountered String.Empty I felt it necessary to check the docs to make sure
              it meant what I thought it did. And most languages have an unambiguous way
              to express an empty string as a constant similar to "", so I prefer "" as
              clearer than String.Empty.

              --Bob

              "Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
              news:%237iEkNlz EHA.3204@TK2MSF TNGP10.phx.gbl. ..[color=blue]
              > which one do you use and why?
              >
              > MyString == null || MyString == ""
              >
              > vs
              >
              > MyString == null || MyString.Length == 0
              >[/color]


              Comment

              • LEBRUN Thomas

                #8
                Re: checking for empty string

                > As an aside, String.Empty is completely equivalent to MyString == "",[color=blue]
                > performance-wise -- String.Empty is simply a language-independent way to
                > express it[/color]

                Not exactly ;)

                The post of Brad Adams
                (http://blogs.msdn.com/brada/archive/.../22/49997.aspx) confirm that
                using "" create an object while String.Empty create no onject.

                So if you are really looking for ultimately in memory efficiency, use
                String.empty

                Bye.

                -------------------
                LEBRUN Thomas



                "Bob Grommes" wrote:
                [color=blue]
                > It's a trade off between a slight advantage in clarity vs a slight advantage
                > in performance (actually MyString == "" has something more than a slight
                > performance advantage in theory, but in practice it isn't that often that it
                > amounts to so much of a difference that a user would actually notice).
                >
                > I personally use MyString.Length == 0 out of habit, as I think it's pretty
                > self-explanatory.
                >
                > As an aside, String.Empty is completely equivalent to MyString == "",
                > performance-wise -- String.Empty is simply a language-independent way to
                > express it. It would also have the advantage of being utterly clear, except
                > that in my experience many developers seem fuzzy on exactly what Empty
                > really means. I mean, *I'm* clear on what it means yet when I first
                > encountered String.Empty I felt it necessary to check the docs to make sure
                > it meant what I thought it did. And most languages have an unambiguous way
                > to express an empty string as a constant similar to "", so I prefer "" as
                > clearer than String.Empty.
                >
                > --Bob
                >
                > "Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
                > news:%237iEkNlz EHA.3204@TK2MSF TNGP10.phx.gbl. ..[color=green]
                > > which one do you use and why?
                > >
                > > MyString == null || MyString == ""
                > >
                > > vs
                > >
                > > MyString == null || MyString.Length == 0
                > >[/color]
                >
                >
                >[/color]

                Comment

                • Bob Grommes

                  #9
                  Re: checking for empty string

                  I take his statement with a grain of salt. String.Empty is a string
                  instance that presumably already exists as he states (it's a static
                  instance), but "" not just "probably" but *wil*" come out of the string
                  intern pool, as do all string constants in programs. As even that blogger
                  admits, the difference would be insignificant in any case. And I've seen
                  more than one other confident statement that String.Empty and "" are exactly
                  identical. Personally I don't have the time to go through the Rotor source
                  to figure it out.

                  The truth is that these are the little rationalization s we create to
                  validate our favorite way of doing things. Value == "", Value ==
                  String.Empty, and String.Length == 0 are in fact all perfectly fine and in
                  almost all real-world scenarios even String.Length == 0 will not produce a
                  *significant* (user-noticeable) difference in performance.

                  Only we geeks can thoroughly debate a topic this unimportant to death ;-)

                  --Bob

                  "LEBRUN Thomas" <lebrun_thomas_ at_hotmail.com> wrote in message
                  news:02784ADA-3E2F-4D51-918E-91780B250DAF@mi crosoft.com...[color=blue][color=green]
                  >> As an aside, String.Empty is completely equivalent to MyString == "",
                  >> performance-wise -- String.Empty is simply a language-independent way to
                  >> express it[/color]
                  >
                  > Not exactly ;)
                  >
                  > The post of Brad Adams
                  > (http://blogs.msdn.com/brada/archive/.../22/49997.aspx) confirm that
                  > using "" create an object while String.Empty create no onject.
                  >
                  > So if you are really looking for ultimately in memory efficiency, use
                  > String.empty
                  >
                  > Bye.
                  >
                  > -------------------
                  > LEBRUN Thomas
                  > http://morpheus.developpez.com
                  > http://blog.developpez.com/index.php?blog=9
                  >
                  > "Bob Grommes" wrote:
                  >[color=green]
                  >> It's a trade off between a slight advantage in clarity vs a slight
                  >> advantage
                  >> in performance (actually MyString == "" has something more than a slight
                  >> performance advantage in theory, but in practice it isn't that often that
                  >> it
                  >> amounts to so much of a difference that a user would actually notice).
                  >>
                  >> I personally use MyString.Length == 0 out of habit, as I think it's
                  >> pretty
                  >> self-explanatory.
                  >>
                  >> As an aside, String.Empty is completely equivalent to MyString == "",
                  >> performance-wise -- String.Empty is simply a language-independent way to
                  >> express it. It would also have the advantage of being utterly clear,
                  >> except
                  >> that in my experience many developers seem fuzzy on exactly what Empty
                  >> really means. I mean, *I'm* clear on what it means yet when I first
                  >> encountered String.Empty I felt it necessary to check the docs to make
                  >> sure
                  >> it meant what I thought it did. And most languages have an unambiguous
                  >> way
                  >> to express an empty string as a constant similar to "", so I prefer "" as
                  >> clearer than String.Empty.
                  >>
                  >> --Bob
                  >>
                  >> "Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
                  >> message
                  >> news:%237iEkNlz EHA.3204@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
                  >> > which one do you use and why?
                  >> >
                  >> > MyString == null || MyString == ""
                  >> >
                  >> > vs
                  >> >
                  >> > MyString == null || MyString.Length == 0
                  >> >[/color]
                  >>
                  >>
                  >>[/color][/color]


                  Comment

                  • C# Learner

                    #10
                    Re: checking for empty string

                    LEBRUN Thomas <lebrun_thomas_ at_hotmail.com> wrote:
                    [color=blue]
                    > So if you are really looking for ultimately in memory efficiency, use
                    > String.empty[/color]

                    FxCop tells me that testing String.Length against 0 is more efficient.

                    Comment

                    • Richard Blewett [DevelopMentor]

                      #11
                      Re: checking for empty string

                      Micro-optimization, while interesting, is almost never a reason to write code in a particular way. Clarity and maintainability are hugely more important to me.

                      Regards

                      Richard Blewett - DevelopMentor



                      LEBRUN Thomas <lebrun_thomas_ at_hotmail.com> wrote:
                      [color=blue]
                      > So if you are really looking for ultimately in memory efficiency, use
                      > String.empty[/color]

                      FxCop tells me that testing String.Length against 0 is more efficient.

                      Comment

                      • LEBRUN Thomas

                        #12
                        Re: checking for empty string

                        > FxCop tells me that testing String.Length against 0 is more efficient.

                        Yes, it's right.

                        But the post was a discussion in what is the best between using "" or
                        String.Empty, not String.Length ;)

                        But, as Richard Blewett said, micro-optimization is not a reason for
                        changing the way you write code.

                        So if reducing execution time of your application is not your main purpose,
                        write your code withour changing your practices

                        Bye

                        -------------------
                        LEBRUN Thomas




                        "C# Learner" wrote:
                        [color=blue]
                        > LEBRUN Thomas <lebrun_thomas_ at_hotmail.com> wrote:
                        >[color=green]
                        > > So if you are really looking for ultimately in memory efficiency, use
                        > > String.empty[/color]
                        >
                        > FxCop tells me that testing String.Length against 0 is more efficient.
                        >[/color]

                        Comment

                        Working...