fearofawhackplanet
fearofawhackplanet

Reputation: 53396

Explicit assignment of null

string s1;
string s2 = null;

if (s1 == null) // compile error
if (s2 == null) // ok

I don't really understand why the explicit assignment is needed. Whats the difference between a null variable and an unassigned variable? I always assumed that unassigned variables were simply assigned as null by the runtime/compiler anyway. If they're not null, then what are they?

Upvotes: 52

Views: 2132

Answers (7)

Daniel Daranas
Daniel Daranas

Reputation: 22624

The C# compiler does not allow the use of uninitialized local variables. An initially unassigned variable has no initial value.

Upvotes: 4

Kirk
Kirk

Reputation: 4560

One thing to keep in mind is that scope will play a part in this. If you had defined S1 as a class variable then tested it inside a function the compiler would not have stopped and the code would run fine. The reason is that the variable is initialized when the class is instantiated.

Move inside a method and there is a good chance that you forgot something when testing the variable before initializing it.

The other caveat that I see is what does a string default to? (And more important is this in a specification that won't change? Keep in mind an empty string is not the same as a null assigned string. There is a way around this though as you can instead test with string.IsNullOrEmpty(S1) instead.

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838376

Unassigned members are automatically initialized to their default values (which is the null reference in the case for string).

Unassigned local variables are not assigned any value and trying to access a possibly unassigned variable will give a compile error.

Upvotes: 75

back2dos
back2dos

Reputation: 15623

A variable that is never assigned a value holds an undefined value. In worst case, which actually occurs in most languages, this means, it can have any value, because it addresses some bit of memory, that is very likely to have been previously used for another purpose, potentially even by another programm. Some languages ensure that all variables are initialized to some sensible defaul values. However this may simply be a waste, since in the end it is a write operation, that may not be neccessary, doing this by default, it is a waste of time.

Upvotes: 2

tanascius
tanascius

Reputation: 53944

Have a look at the spec: 5.3 Definite assignment

Definite assignment is a requirement in the following contexts:
A variable must be definitely assigned at each location where its value is obtained.

s1 and s2 are initially unassigned (5.3.1 Initially assigned variables), but only s2 is considered definitely assigned at a given location, [because] all possible execution paths leading to that location contain at least one of the following:

  • A simple assignment (Section 7.13.1) in which the variable is the left operand.

As you can see, null is irrelevant in this context. Important is the assignment itself, but not the value.

Upvotes: 4

Jon Hanna
Jon Hanna

Reputation: 113292

  1. If you have an unassigned local value, you are quite likely doing something stupid. Worse of all you are doing the sort of stupid thing that smart people can do in the heat of the moment, (everyone does something stupid every day).

  2. Unlike some things that result in warnings, there's no possible advantage of using an unassigned value in a particular remarkable case.

  3. The only cost difference between allowing an unassigned local or assuming a particular value, is a few keystrokes (normally = null; at most it could be = default(SomeType);

Banning such constructs is heavy on pros and low on cons. There's no technical reason why the language couldn't have been designed to allow unassigned locals, but the benefits of banning outweigh the disadvantages.

Upvotes: 7

Tseng
Tseng

Reputation: 64180

The reason why explicit assignment is required is quite simple. This often a source of errors when people try to use unassigned/uninitialized variables.

By forcing the developer to do this, it eliminates errors which happen when the developer forgets to initialize the variable. And by initializing it, you're in control of it.

It's a good thing really! I dunno how often I had uninitialized or undefined variables in some of the scripting languages which took quite some time to be found ^^

Upvotes: 13

Related Questions