15

I am aware that I can set null like

string val = null;

But I am wondering the other ways I can set it to null. Is there a funcion like String.null that I can use.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Ajax3.14
  • 1,647
  • 5
  • 24
  • 42
  • 9
    Why would you need another way to do it? – Charleh Jun 25 '12 at 17:09
  • 1
    why would doing string val = String.null be any better.. nulls have very little to do with strings – Osama Javed Jun 25 '12 at 17:09
  • You mean a `static void StringUtils.SetNull(out string s)` that sets `s` to `null`? That would be otherkill IMHO. What's wrong with assignment? – Frédéric Hamidi Jun 25 '12 at 17:09
  • @dash Say I changed it to something and then set it back to null... – Ajax3.14 Jun 25 '12 at 17:10
  • @Ajax3.14 - I'd just set it to `null` as you've done, or even `String.Empty`. Then, if I cared, at least I can check `String.IsNullOrEmpty(val);` – dash Jun 25 '12 at 17:13
  • `string val` in your example is not a string. It's a reference to a string object. You might want to read up on how objects are handled in C#. – Wormbo Jun 25 '12 at 17:13
  • Please note that string.Empty is not the same thing as null. String.Empty is equal to the zero length string "". – Seth Flowers Jun 25 '12 at 18:12

7 Answers7

28

I think you are looking far String.Empty

string val = String.Empty;

Update: TheFuzzyGiggler comments are worth mentioning:

It's much better to set a string to empty rather than null. To avoid exceptions later. If you have to read and write a lot of strings you'll have null value exceptions all over the place. Or you'll have a ton of if(!string.isNullorEmpty(string)) which get annoying

Community
  • 1
  • 1
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
  • 6
    Beat me to it! If OP reads this.. I've found it's MUCH better to set a string to empty rather than null.. To avoid exceptions later. If you have to read and write a lot of strings you'll have null value exceptions all over the place. Or you'll have a ton of if(!string.isNullorEmpty(string)) which get annoying. – TheFuzzyGiggler Jun 25 '12 at 17:16
  • 6
    Thanks for the props. Just glad I can help. I'll let Asif take the rep glory. =) – TheFuzzyGiggler Jun 25 '12 at 17:34
  • 1
    Null and String.Empty are certainly not interchangeable. If your system considers them the same, then by all means, use Empty but they are not the same. You can't just willy nilly use one where you can use the other. There is a difference between a user typing in an empty string and a user not typing in anything – Joe Phillips Jul 21 '17 at 20:59
16

You can also use the default keyword.

string val = default(string);

Here is another SO question regarding the default keyword: What is the use of `default` keyword in C#?

Community
  • 1
  • 1
Seth Flowers
  • 8,990
  • 2
  • 29
  • 42
  • 3
    ...which is the same as `string val = (string)null;`, i.e. a typed null reference. – Wormbo Jun 25 '12 at 17:13
  • @Wormbo - correct. The nice thing about using `default` though, is that it works with both value types and reference types. – Seth Flowers Feb 13 '17 at 19:19
4

null is not a special string value (there's String.Empty for ""), but a general object literal for the empty reference.

Wormbo
  • 4,978
  • 2
  • 21
  • 41
0

Obviously one more way to set something to null is to assign result of function that returns null:

string SomeFunctionThatReturnsNullString() {return null;}

string val = SomeFunctionThatReturnsNullString();
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

assign it to this value will make it a null ,(chose the right sql datatype , here its double )

    System.Data.SqlTypes.SqlDouble.Null
Wesam
  • 932
  • 3
  • 15
  • 27
0

I frequently run into this issue when making updates to databases. Especially when the column is a date. If you pass "" or String.Empty, the date is set to MinDate which usually displays as 01/01/1900. So Null is a preferred value when you want No date.

I have had to make sure I capture the "" case and substitute the DBNull.Value which will pass a Null value back to the DB.

if (!string.IsNullOrEmpty(this.DeletedDate)) { 
                db.AddParameter("newDeletedDate", this.DeletedDate);
            }
            else
            {
                db.AddParameter("newDeletedDate", DBNull.Value);
            }
Tim Melton
  • 337
  • 3
  • 6
-1

Depending on your application you could use string.Empty. I'm always leery of initializing to null as the garbage collector is great, but not perfect, and could mark your string for garbage collection if used in some way that the garbage collector can't detect (this is very unlikely though).

Joel B
  • 12,082
  • 10
  • 61
  • 69