ingredient_15939
ingredient_15939

Reputation: 3134

In .NET, what is the rationale for Strings initializing to null?

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. :)

Upvotes: 5

Views: 173

Answers (4)

xanatos
xanatos

Reputation: 111840

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)

Upvotes: -1

Mark Byers
Mark Byers

Reputation: 838066

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.

Upvotes: 4

Ilian
Ilian

Reputation: 5355

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.

Upvotes: 0

Matthew
Matthew

Reputation: 13332

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.

Upvotes: 7

Related Questions