13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

What do you do when you just don’t have a value?1134.1 What do you do when you just don’t have a value?The <strong>C#</strong> and .NET designers don’t add features just for kicks. There has to be a real, significantproblem to be fixed before they’ll go as far as chang<strong>in</strong>g <strong>C#</strong> as a language or.NET at the platform level. In this case, the problem is best summed up <strong>in</strong> one of themost frequently asked questions <strong>in</strong> <strong>C#</strong> and .NET discussion groups:I need to set my DateTime 1 variable to null, but the compiler won’t let me.What should I do?It’s a question that comes up fairly naturally—a simple example might be <strong>in</strong> ane-commerce application where users are look<strong>in</strong>g at their account history. If an orderhas been placed but not delivered, there may be a purchase date but no dispatchdate—so how would you represent that <strong>in</strong> a type that is meant to provide theorder details?The answer to the question is usually <strong>in</strong> two parts: first, why you can’t just use null<strong>in</strong> the first place, and second, which options are available. Let’s look at the two parts separately—assum<strong>in</strong>gthat the developer ask<strong>in</strong>g the question is us<strong>in</strong>g <strong>C#</strong> 1.4.1.1 Why value type variables can’t be nullAs we saw <strong>in</strong> chapter 2, the value of a reference type variable is a reference, and thevalue of a value type variable is the “real” value itself. A “normal” reference value issome way of gett<strong>in</strong>g at an object, but null acts as a special value that means “I don’trefer to any object.” If you want to th<strong>in</strong>k of references as be<strong>in</strong>g like URLs, null is (veryroughly speak<strong>in</strong>g) the reference equivalent of about:blank. It’s represented as allzeroes <strong>in</strong> memory (which is why it’s the default value for all reference types—clear<strong>in</strong>ga whole block of memory is cheap, so that’s the way objects are <strong>in</strong>itialized), but it’s stillbasically stored <strong>in</strong> the same way as other references. There’s no “extra bit” hiddensomewhere 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 go<strong>in</strong>g to run out longbefore 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’sconsider the byte type as a familiar one that is easy to th<strong>in</strong>k about. The value of a variableof type byte is stored <strong>in</strong> a s<strong>in</strong>gle byte—it may be padded for alignment purposes,but the value itself is conceptually only made up of one byte. We’ve got to be able tostore the values 0–255 <strong>in</strong> that variable; otherwise it’s useless for read<strong>in</strong>g arbitraryb<strong>in</strong>ary data. So, with the 256 “normal” values and one null value, we’d have to copewith a total of 257 values, and there’s no way of squeez<strong>in</strong>g that many values <strong>in</strong>to a s<strong>in</strong>glebyte. Now, the designers could have decided that every value type would have anextra flag bit somewhere determ<strong>in</strong><strong>in</strong>g whether a value was null or a “real” value, butthe memory usage implications are horrible, not to mention the fact that we’d have tocheck the flag every time we wanted to use the value. So <strong>in</strong> a nutshell, with value types1It’s almost always DateTime rather than any other value type. I’m not entirely sure why—it’s as if developers<strong>in</strong>herently understand why a byte shouldn’t be null, but feel that dates are more “<strong>in</strong>herently nullable.”Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!