0
   what is the difference among these 

   1. string str=string.Empty;
   2. string str="";
   3. string str=null;   

and is there any different way to use these statements...

thanks saj

John Saunders
  • 160,644
  • 26
  • 247
  • 397
selvaraj
  • 889
  • 2
  • 16
  • 29

2 Answers2

3

string.Empty and "" both end up as references to a "real" string object which happens to be empty. So you can call methods on it, use its Length property etc.

The null reference is a value which doesn't refer to any object. If you try to dereference null, you'll get a NullReferenceException.

As to whether you choose "" or string.Empty - that's really a matter of personal preference. I tend to choose "", but others find string.Empty more readable. Note that while "" is a constant expression, string.Empty isn't, so you can't use the latter in case statements.

EDIT: Note that both of the other answers present at the time of this edit imply that null is not a value, or that the variable hasn't been assigned a value. This is incorrect. null is a value just like any other - it's just that it doesn't refer to any object. In particular, assigning null to a variable will overwrite any previous value, and if it's a local variable that assignment means the variable has been "definitely assigned". For example, compare these two snippets

string s;
Console.WriteLine(s); // Compile-time error: s isn't definitely assigned

string s = null;
Console.WriteLine(s); // Fine: s has the value null
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • From what I understand if you use String.Empty no object is created while an empty string "" will most likely result in a string being interned. – Mike Cheel Oct 06 '10 at 12:46
  • @Mike: Well what object do you think `String.Empty` refers to? Clearly an object is created at *some* point. In fact, the two end up referring to the same string object these days. There was a difference pre-2.0, but it's still nothing worth being concerned about. – Jon Skeet Oct 06 '10 at 12:47
  • @Jon Skeet: Not entirely true. The constant or literal empty string (i.e. "") would be created once into every .NET component that uses it, whether it's the original created version or the intern (same thing I know). Using String.Empty, it is loaded once into the SharedDomain (as opposed to the AppDomain) and will not be recreated when multiple applications use it. – Mike Cheel Oct 06 '10 at 15:28
  • @Mike: When you say "every .NET component that uses it" what exactly do you mean? Whether "" is shared between AppDomains or not is up to some setting that I can't remember, but every class within the same AppDomain (or *certainly* the same assembly) should end up using the same one. Basically this *isn't* going to cause a memory problem in *any* sensible application. The difference is so negligible, it's not worth considering as part of the decision about whether to use `""` or `string.Empty`, IMO. – Jon Skeet Oct 06 '10 at 15:38
  • @Jon Skeet: All I am saying is that under the hood there is actually a difference between empty quotes and String.Empty. If you use empty quotes then you will have one intern per application (app domain). If you use String.Empty then you have one intern amongst one or more apps (shared domain). I'm not disputing that there is or is not a performance hit. It doesn't matter as far as performance goes. – Mike Cheel Oct 06 '10 at 17:25
  • @Mike: I agree that there's a miniscule difference. I just believe that it's insignificant in just about every imaginable scenario... which means it really *is* a matter of personal preference. Which bit of what I wrote is "not entirely true"? I'll correct what I can... – Jon Skeet Oct 06 '10 at 18:03
  • I was referring to what I perceived as an insinuation that what I had said was incorrect. I guess I could have been clearer. As far as personal preference I will agree with you. My personal preference is to go with String.Empty – Mike Cheel Oct 06 '10 at 18:31
  • @Mike: Well, I think it's incorrect to say "if you use String.Empty no object is created" - because clearly it's going to create an object at some point. It refers to an object, therefore an object must have been created. However, it's not created every time through the code - which is also the case with "" either. Many people have misinterpreted a blog post from Brad Abrams a long time ago as saying that every time you go through code using "", it creates a new string. – Jon Skeet Oct 06 '10 at 18:42
  • @Jon Skeet - And that is why I said I could have been clearer. Not incorrect though, incomplete. – Mike Cheel Oct 06 '10 at 19:24
  • @Mike: I think at this point we agree on everything that's actually important :) – Jon Skeet Oct 06 '10 at 19:32
2

string.Empty is a different way of saying "". (according to Jon Skeet, use whatever is common in your team). And null means it wasn't assigned a value at all.
(But the Skeet is here to explain that himself (-: )

Community
  • 1
  • 1
Oren A
  • 5,870
  • 6
  • 43
  • 64
  • 1
    `string.Empty` is a *different* way of saying `""`. It might be better for you; it's not for me ;) – LukeH Oct 06 '10 at 12:43
  • hi Oren it's good can you describe how you are saying string.Empty is a better way to this "" – selvaraj Oct 06 '10 at 12:44
  • I agree with Oren that `string.Empty` conveys more meaning. Some people say that it is too easy to type `" "` instead of `""` (note the space). – Oded Oct 06 '10 at 12:45
  • I would also argue that assigning a value of `null` *is* assigning a value. It's a value which refers to no object, but that's very different to not assigning any value. In particular, if this is a local variable, it's definitely assigned after setting it to null, whereas it wouldn't be without the assginment. – Jon Skeet Oct 06 '10 at 12:46
  • @Oded: Have those people ever *actually* accidentally typed " " instead of ""? – Jon Skeet Oct 06 '10 at 12:46
  • @Oded: So you agree with those that say `string.Empty` conveys more meaning, *and* you also agree that it's a silly argument? – LukeH Oct 06 '10 at 12:52
  • @LukeH - I agree that it conveys more meaning, but that the claim that one might type `" "` instead of `""` is a silly argument. – Oded Oct 06 '10 at 12:53
  • 1
    @Oded: Fair enough, although I personally find it less readable (or, at least, that it requires more cognitive effort to decipher). Does something like `Number.Three` carry more meaning than just plain `3`? – LukeH Oct 06 '10 at 12:57
  • @LukeH - it's all about what one is used to and what the team decided. – Oded Oct 06 '10 at 12:58