3

Is there any need to set a string member variable of a class to ""? Will it be null unless you do this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
CJ7
  • 22,579
  • 65
  • 193
  • 321
  • Check this also http://stackoverflow.com/questions/265875/default-string-initialization-null-or-empty – Amr Badawy Jun 30 '10 at 04:36
  • I need some way to break the tie between Jerod and jean27. – CJ7 Jul 01 '10 at 02:12
  • 1
    accept Jerod's answer, he answered first ... – Hinek Jul 02 '10 at 07:55
  • @Hinek: how can you tell which was first? They both say 2 days (at time of writing this). – CJ7 Jul 02 '10 at 09:08
  • between the question and the answers is a thin line "3 Answers" - "oldest" - "newest" - "votes". If you click on "oldest" the answers are sorted oldest on top (exception is the accepted answer, it's always on top). – Hinek Jul 02 '10 at 10:51

6 Answers6

19

The reason a string will be null is because a string is a reference type and the default value for all reference types is null (i.e. no reference).

The best reference is the Default Values Table (remember all reference types are set to null by default) but some examples below give a decent idea:

string a = default(string); // null
String b = default(String); // null
int c = default(int); // 0
int? d = default(int?); // null
Object e = default(object); // null
double f = default(double); // 0.0

// an instance of someStruct with fields set to default(theirType)
someStruct g = default(someStruct); 

A string kind of seems to be a value type because it seems so primitive. Additionally it is immutable meaning that you can never modify a string, instead you just set string symbols to point to new string "values" (which are themselves references), giving the impression that string is a value type.

For example:

 string a = "boo"; 
 /* Mem contains reference to "boo" and symbol 'a' => reference("boo") */

 a = "gah";
 /* Mem contains references to "boo" and "gah". symbol 'a' -> reference("gah") */

For more info on strings see this great article.

And don't forget the Default Values Table.

Matt Mitchell
  • 40,943
  • 35
  • 118
  • 185
6

Yes, the default value of a string is null.

Jerod Houghtelling
  • 4,783
  • 1
  • 22
  • 30
6

A string's default value is null.

jean27
  • 701
  • 8
  • 25
1

This has already been asked and answered many times. And the previous answerers here have jumped straight to your second question without answering your first, which I feel is the more important since a simple Google search would have told you what the default is.

"Is there any need to set a string member variable of a class to ""?"

It depends on what you mean by "need". If you are into readability then yes, there could very well be a need. It will be a personal decision that requires a consensus decision by your team. I personally find things much more readable if variables are given a default value in an appropriate place. If you do it in the right place the compiler will (can) optimize it away, so it really comes down to readability.

Some excellent insight can be found here.

Community
  • 1
  • 1
sliderhouserules
  • 3,415
  • 24
  • 32
  • 4
    And if you "need" to set strings to `""` to be clear, then you might as well follow StyleCop's advice and use `string.Empty` instead. – Daniel Pryden Jul 06 '10 at 06:18
  • I'm not advocating between "" and null, just between nothing and anything. If a variable is never initialized, then it requires that very short but inherently interruptive pause to think about what the default is when a variable isn't set. Eliminating these interruptions (non-initialized variables are just one example, and a minor one at that) is one of the essences of readability, in my mind. – sliderhouserules Jul 06 '10 at 06:22
1
  • Is there any need to set a string member variable of a class to ""?

Not if your class methods know that the default value of a string member variable is null and deal with it accordingly (i.e., check it for null before using its value).

Otherwise, it is probably a good idea to initialize it to some non-null value. This can simplify your methods if they can assume that the variable has a non-null value at all times.

  • Will it be null unless you do this?

Yes, a member variable of reference type (e.g., string) without an explicit initializer value is initialized to null by default when the class object is constructed.

David R Tribble
  • 11,918
  • 5
  • 42
  • 52
1

Also, if your app does not distinguish between null and empty "", then when you test the value, use String.IsNullOrEmpty(). If you also treat whitespace the same as empty, then use the new String.IsNullOrWhiteSpace(). null, "", and " " will be true. Any non-whitespace characters will be false.

string s = GetSomeString();

if(String.IsNullOrEmpty(s)) {
//do whatever here
}
Matt Dotson
  • 5,887
  • 1
  • 23
  • 23