J
Jon Skeet [C# MVP]
Well, it's a bit more complicated than that. If it's a parameter, for
example, the compiler doesn't complain at all. Example:
public boo StringIsNull(string s)
{
return (s == null);
}
Yes, s is definitely assigned, according to the spec:
<quote>
The following categories of variables are classified as initially
assigned:
[...]
* 6 Value parameters.
</quote>
In the above, s is a value parameter. So it is definitely assigned,
regardless of its value.
An "out" parameter, however, *isn't* definitely assigned at the start
of the method, so it would fail to compile - the variable would not
have a value which could be retrieved under the rules of the language.
Also, note that I said "Null is not a value technically speaking." I
qualified my remark because it depends on what is meant by the term "value."
Of course, null must be represented by a "value" in memory, but it
represents the absence of a value.
No, it represents the absence of an *instance* that value refers to.
And as you pointed out, if a member is
declared and not initialized, it is "initialized" as null. But that holds
true for value types as well as reference types. So, what we're seeing here
is more a decision on the part of the designers of the compiler, rather than
a "physical" reality.
I'm talking on the language level more than anything else - where null
is clearly defined to be a value which doesn't refer to any instance.
They could just as easily have assumed a null value
for a local variable, but decided not to.
They could have done - but only if they'd made sure that whatever the
compiler was targetting would give local variables the value "null" to
start with. (IIRC, this is true for the CLI.)
This is, of course, one of the problems with human language. It is subject
to interpretation. So, in fact, I don't think either of us is wrong about
what we are saying. One or both of us is just not trying hard enough to
understand the other!
Well, that's one of the purposes of specifications - to define what
terms mean as far as possible. In this case, the C# spec defines both
"null" and "unassigned variable" - and they are *not* the same thing
at all.
Jon