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.Empty and null

string.Empty and null

Scheduled Pinned Locked Moved C#
11 Posts 7 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.
  • B Offline
    B Offline
    binglin
    wrote on last edited by
    #1

    wad's the difference between: string a = string.Empty; string a = null;

    R R E 4 Replies Last reply
    0
    • B binglin

      wad's the difference between: string a = string.Empty; string a = null;

      R Offline
      R Offline
      Rob Graham
      wrote on last edited by
      #2

      String.Empty is a defined value for a string instance of zero length, it is not null. string a = null is an uninitialized object of type string, but since it is uninitialized, it is not valid to reference (cant't call any methods on it, or reference it without an exception being raised). string a = null; string b = string.Empty; a.Compare(b); //raises null ref exception b.Compare(a); //raises null reference exception string c = "x"; c.Compare(a); // null reference c.Compare(b); //valid statement Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke

      1 Reply Last reply
      0
      • B binglin

        wad's the difference between: string a = string.Empty; string a = null;

        R Offline
        R Offline
        radic feng
        wrote on last edited by
        #3

        In my eye. string a = string.Empty; is the same as string a = "";

        D R E 3 Replies Last reply
        0
        • R radic feng

          In my eye. string a = string.Empty; is the same as string a = "";

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          They are equal, but neither one is null. Both of your code samples represent an instantiated object of type string holding a value of zero length. You can call methods and get/set properties on instantiated objects. A string cal also be null, in which case it doesn't refer to any instantiated object. Therefore, you can't call any methods or modify any properties on it. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome

          1 Reply Last reply
          0
          • R radic feng

            In my eye. string a = string.Empty; is the same as string a = "";

            R Offline
            R Offline
            Rob Graham
            wrote on last edited by
            #5

            Yes and no. String.Empty is a defined constant contained in the string class. "" is defining a new string value of zero length in your own class, which is equivalent to srting.empty. The compiler may well just use string.Empty here, or it may intern a new string value in your assembly. It is probably better practice to use the existing string constant (string.Empty) when initializing strings, since it is certain what happens. Absolute faith corrupts as absolutely as absolute power Eric Hoffer All that is necessary for the triumph of evil is that good men do nothing. Edmund Burke

            1 Reply Last reply
            0
            • R radic feng

              In my eye. string a = string.Empty; is the same as string a = "";

              E Offline
              E Offline
              eggie5
              wrote on last edited by
              #6

              In my mind's eye Horatio... /\ |_ E X E GG

              1 Reply Last reply
              0
              • B binglin

                wad's the difference between: string a = string.Empty; string a = null;

                E Offline
                E Offline
                eggie5
                wrote on last edited by
                #7

                I think a better question would be.. wad's the difference between: string a=String.Empty; string a=""; /\ |_ E X E GG

                R 1 Reply Last reply
                0
                • B binglin

                  wad's the difference between: string a = string.Empty; string a = null;

                  R Offline
                  R Offline
                  radic feng
                  wrote on last edited by
                  #8

                  Hot discussion! I have another topic: what's the difference between "==" and method "Equal". are they same? It is quite confusing! In jave language, "two object Equal" means the refs pointing to the same memory address. But in C#, I find it is different. You can use if( stringA == "aa" ) instead of if( stringA.Equal("aa" ); Any ideas? Radic Feng Beijing, China.

                  U 1 Reply Last reply
                  0
                  • R radic feng

                    Hot discussion! I have another topic: what's the difference between "==" and method "Equal". are they same? It is quite confusing! In jave language, "two object Equal" means the refs pointing to the same memory address. But in C#, I find it is different. You can use if( stringA == "aa" ) instead of if( stringA.Equal("aa" ); Any ideas? Radic Feng Beijing, China.

                    U Offline
                    U Offline
                    Uri Lavi
                    wrote on last edited by
                    #9

                    * Operator equality in C# compares refs pointing while Equals compares values equality. BUT: * For strings Operator equality in C# performs value type equality meaning it actually compares the values in the addresses. (Partially it's due the fact that strings are immutable in C# and usually we want actually to compare the values and not the addresses of the instances). In C# Operator equality makes internal call to string.Equals (thus results in slightly poorer performance).

                    R 1 Reply Last reply
                    0
                    • U Uri Lavi

                      * Operator equality in C# compares refs pointing while Equals compares values equality. BUT: * For strings Operator equality in C# performs value type equality meaning it actually compares the values in the addresses. (Partially it's due the fact that strings are immutable in C# and usually we want actually to compare the values and not the addresses of the instances). In C# Operator equality makes internal call to string.Equals (thus results in slightly poorer performance).

                      R Offline
                      R Offline
                      radic feng
                      wrote on last edited by
                      #10

                      Many thanks!

                      1 Reply Last reply
                      0
                      • E eggie5

                        I think a better question would be.. wad's the difference between: string a=String.Empty; string a=""; /\ |_ E X E GG

                        R Offline
                        R Offline
                        Rei Miyasaka
                        wrote on last edited by
                        #11

                        The answer: nothing. String.Empty is defined as "" in System.String's static constructor, and it's a static readonly field. By the way, .NET 2.0 has a string.IsNullOrEmpty function, which is sorta cool.

                        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