Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. String.IsNullOrEmpty

String.IsNullOrEmpty

Scheduled Pinned Locked Moved C#
performancehelp
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nasambur
    wrote on last edited by
    #1

    Hi, Which one gives better performance, checking a string against null & empty. string s = "something";for(int i=0; i<100; i++) { if(s != null) { if(s.Length > 0) { // do something } } }
    orfor(int i=0; i<100; i++) { if(!String.IsNullOrEmpty(s)) { // do something } }
    plz help, nas

    O U C 3 Replies Last reply
    0
    • N nasambur

      Hi, Which one gives better performance, checking a string against null & empty. string s = "something";for(int i=0; i<100; i++) { if(s != null) { if(s.Length > 0) { // do something } } }
      orfor(int i=0; i<100; i++) { if(!String.IsNullOrEmpty(s)) { // do something } }
      plz help, nas

      O Offline
      O Offline
      originSH
      wrote on last edited by
      #2

      put it in a loop that will run a few hundred thousand times and use the System.Diagnostics.StopWatch[^]. It's a good idea to get used to testing things like this yourself ... it's quicker than asking on here, more reliable and you have more chance of getting an answer with some of the more obscure stuff (not that IsNullOrEmpty is obsucre).

      E 1 Reply Last reply
      0
      • N nasambur

        Hi, Which one gives better performance, checking a string against null & empty. string s = "something";for(int i=0; i<100; i++) { if(s != null) { if(s.Length > 0) { // do something } } }
        orfor(int i=0; i<100; i++) { if(!String.IsNullOrEmpty(s)) { // do something } }
        plz help, nas

        U Offline
        U Offline
        Urs Enzler
        wrote on last edited by
        #3

        Or use Reflector to check what String.isNullOrEmpty does:

        public static bool IsNullOrEmpty(string value)
        {
        if (value != null)
        {
        return (value.Length == 0);
        }
        return true;
        }

        IsNullOrEmpty is therefore just a bit slower because there is a strack frame more present (for calling the method). But compared to the time you are using in writing that every time, I'd use IsNullOrEmpty ;)

        -^-^-^-^-^- no risk no funk ................... please vote ------>

        1 Reply Last reply
        0
        • N nasambur

          Hi, Which one gives better performance, checking a string against null & empty. string s = "something";for(int i=0; i<100; i++) { if(s != null) { if(s.Length > 0) { // do something } } }
          orfor(int i=0; i<100; i++) { if(!String.IsNullOrEmpty(s)) { // do something } }
          plz help, nas

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          Seriously, for the additional fractions of a microsecond that IsNullOrEmpty takes I wouldn't worry about it.


          Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

          P 1 Reply Last reply
          0
          • C Colin Angus Mackay

            Seriously, for the additional fractions of a microsecond that IsNullOrEmpty takes I wouldn't worry about it.


            Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Won't .Length check for null again anyway? If so, you're checking for null twice. I'd just use IsNullOrEmpty figuring Microsoft wouldn't steer me wrong. :-D

            C 1 Reply Last reply
            0
            • P PIEBALDconsult

              Won't .Length check for null again anyway? If so, you're checking for null twice. I'd just use IsNullOrEmpty figuring Microsoft wouldn't steer me wrong. :-D

              C Offline
              C Offline
              Colin Angus Mackay
              wrote on last edited by
              #6

              PIEBALDconsult wrote:

              Won't .Length check for null again anyway? If so, you're checking for null twice.

              .Length cannot operate on null so it will throw an exception.


              Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

              P 1 Reply Last reply
              0
              • C Colin Angus Mackay

                PIEBALDconsult wrote:

                Won't .Length check for null again anyway? If so, you're checking for null twice.

                .Length cannot operate on null so it will throw an exception.


                Upcoming FREE developer events: * Glasgow: Agile in the Enterprise Vs. ISVs, db4o: An Embeddable Database Engine for Object-Oriented Environments, Mock Objects, SQL Server CLR Integration, Reporting Services ... My website

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                ::Smacking forehead with heel of hand::

                1 Reply Last reply
                0
                • O originSH

                  put it in a loop that will run a few hundred thousand times and use the System.Diagnostics.StopWatch[^]. It's a good idea to get used to testing things like this yourself ... it's quicker than asking on here, more reliable and you have more chance of getting an answer with some of the more obscure stuff (not that IsNullOrEmpty is obsucre).

                  E Offline
                  E Offline
                  ednrgc
                  wrote on last edited by
                  #8

                  But he doesn't want to do any of the work himself. He needs someone to do his homework form him.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups