5

I understand that String is an object, as opposed to say Int, and object variables generally don't point to anything unless you create the object. So I know how it works.

But, in terms of simple, practical programming, why doesn't .NET initialise a string as empty (""), as that is surely the most common use of the string variable - as a string, not as an object?

I've never (yet) had need for a string to be null, that is "not a string". It seems to be a rare requirement. I'm unable to say an int variable is null, so why do so with string? Having it as an option is undoubtedly necessary, but why does .NET make it null to begin with, when int etc. are given proper initial values?

I'm asking because I like to understand not just how something works but why, as it reduced my desire to rant about how weird it seems. :)

Upendra Chaudhari
  • 6,473
  • 5
  • 25
  • 42
ingredient_15939
  • 3,022
  • 7
  • 35
  • 55
  • You haven't had a need for distinguishing between "no string" and "empty string"? That's really strange; what kind of code do you write? For example, there is no string before reading something in from the user; but there is an empty string afterward, if the user chose to enter nothing. – Domenic Oct 14 '11 at 04:47

4 Answers4

7

Strings are not initialized as null. You are confusing initialization with declaration. Whenever you declare a variable like:

 Foo myFoo;

It starts out as null until you actually call its constructor. Like so:

Foo myFoo = new Foo();

Now traditionally you don't call constructors for the string class, you instantiate them with string literals like this:

string myString = "foo";

There are constructors for the string class though, a few of them, but they all take some array of characters / bytes to use to create the string. What never happens is this:

// initialize an empty string ("")
string myString = new string();

This is because the string class is immutable, that is once it has been instantiated you can never change it, you can only create a new string and set your variable to point to it (this is why all the methods of the string class return a new string rather than changing the one you called the method on). So really it makes no sense to initialize to some default value because once you had done so you would be stuck with it.

Matthew
  • 12,892
  • 6
  • 42
  • 45
  • 3
    If string is member of a class it is initialized to null even if you don't explicitly give it a literal value – Muhammad Hasan Khan Oct 14 '11 at 05:20
  • 3
    +1, although if you declare an object in a method body, it is uninitialized, not null. – Jim Mischel Oct 14 '11 at 05:26
  • @Hasan No, again here you are using the word initialized incorrectly. **NULL** is a general concept in computer science. It is used to signify that a pointer is uninitialized and does not point to anything. When you create an instance of a class any member fields that are not initialized (have their values set or constructor called) will be `null` identifying them as uninitialized. Here: http://en.wikipedia.org/wiki/Null_pointer#Null_pointer – Matthew Oct 14 '11 at 05:30
4

String is a reference type. The default value for all reference types is null. This is defined by the C# language and there is no way to change this behaviour.

Even if the designers of the String class wanted the value of default(String) to be the empty string (which I doubt) it's simply not possible to change it.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

Because string is a reference type and reference types default to null. It's just consistent behavior. I would be surprised if it defaulted to something else.

I can think of an example where null strings as the default value actually makes sense. Let's say you are parsing the following XML nodes for the name attribute.

<node name=""/>
<node />

Given the following struct, how can one differentiate the 2 nodes?

struct Node
{
  public string Name { get; }
}

For <node name=""/>, your parser sees the name attribute and then sets Node.Name to string.Empty. For <node />, your parser doesn't see the name attribute and doesn't assign anything.

Thus you're left with Node.Name==string.Empty for the first node and the second node would have Node.Name==null. Which makes sense.

Ilian
  • 5,113
  • 1
  • 32
  • 41
  • Don't give this consistence explanation. `Although string is a reference type, the equality operators (== and !=) are defined to compare the values of string objects, not references. ` Decisions are not always made to be consistent. – manojlds Oct 14 '11 at 04:53
  • Ok, -1 for the example. The question is not about string being null. But why the initialization is null. And read this line from OP - `Having it as an option is undoubtedly necessary, but why does .NET make it null to begin with, when int etc. are given proper initial values?` – manojlds Oct 14 '11 at 04:55
  • @manojlds: About the example code, the null default makes sense because you don't have to assign something to Name if you can't see the name attribute. I've edited my answer to hopefully make this clearer. – Ilian Oct 14 '11 at 05:13
  • I still don't agree with the example in this context, but have removed my downvote. – manojlds Oct 14 '11 at 05:19
-1

The question is connected to another question Why is string a reference type?

When they choosed strings to be reference types, clearly they had to make default(string) == null, in the same way that default(any reference type) == null.

A more intelligent question is then "why are strings reference types instead of, for example, a struct String { private char[] MyInternalSting; }? Because they decided so (even the link that I have given gives more cyrcular thinking logic than real reasons... Like "because otherwhise you couldn't do Object.ReferenceEquals" as if you could do Object.ReferenceEquals with numbers and other value types).

Some other intelligent questions could be why "A" + null == "A" instead of NullPointerException or ArgumentNullException? (i wasn't able to find an intelligent response... Perhaps for compatibility with VB.NET)

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280