Let??™s look at the two parts separately
??”assuming that the developer asking the question is using C# 1.
4.1.1 Why value type variables can??™t be null
As we saw in chapter 2, the value of a reference type variable is a reference, and the
value of a value type variable is the ???real??? value itself. A ???normal??? reference value is
some way of getting at an object, but null acts as a special value that means ???I don??™t
refer to any object.??? If you want to think of references as being like URLs, null is (very
roughly speaking) the reference equivalent of about:blank. It??™s represented as all
zeroes in memory (which is why it??™s the default value for all reference types??”clearing
a whole block of memory is cheap, so that??™s the way objects are initialized), but it??™s still
basically stored in the same way as other references. There??™s no ???extra bit??? hidden
somewhere for each reference type variable. That means we can??™t use the ???all zeroes???
value for a ???real??? reference, but that??™s OK??”our memory is going to run out long
before we have that many live objects anyway.
The last sentence is the key to why null isn??™t a valid value type value, though. Let??™s
consider the byte type as a familiar one that is easy to think about. The value of a variable
of type byte is stored in a single byte??”it may be padded for alignment purposes,
but the value itself is conceptually only made up of one byte.
Pages:
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242