13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Value types and reference types53passed by reference or the value of the argument (the reference) is passed by value.Aside from anyth<strong>in</strong>g else, this answers the question of what happens when null isused as a by-value argument—if objects were be<strong>in</strong>g passed around, that would causeissues, as there wouldn’t be an object to pass! Instead, the null reference is passed byvalue <strong>in</strong> just the same way as any other reference would be.These myths aren’t the only ones around. Box<strong>in</strong>g and unbox<strong>in</strong>g come <strong>in</strong> for theirfair share of misunderstand<strong>in</strong>g, which I’ll try to clear up next.2.3.4 Box<strong>in</strong>g and unbox<strong>in</strong>gSometimes, you just don’t want a value type value. You want a reference. There are anynumber of reasons why this can happen, and fortunately <strong>C#</strong> and .NET provide a mechanism,called box<strong>in</strong>g, that lets you create an object from a value type value and use areference to that new object. Before we leap straight <strong>in</strong>to an example, let’s start off byreview<strong>in</strong>g two important facts:■■The value of a reference type variable is always a reference.The value of a value type variable is always a value of that type.Given those two facts, the follow<strong>in</strong>g three l<strong>in</strong>es of code don’t seem to make muchsense at first glance:<strong>in</strong>t i = 5;object o = i;<strong>in</strong>t j = (<strong>in</strong>t) o;We have two variables: i is a value type variable, and o is a reference type variable. Howdoes it make sense to assign the value of i to o? The value of o has to be a reference,and the number 5 isn’t a reference—it’s an <strong>in</strong>teger value. What’s actually happen<strong>in</strong>g isbox<strong>in</strong>g: the runtime creates an object (on the heap—it’s a normal object) that conta<strong>in</strong>sthe value (5). The value of o is then a reference to that new object. The third l<strong>in</strong>eperforms the reverse operation—unbox<strong>in</strong>g. We have to tell the compiler which type tounbox the object as, and if we use the wrong type (if it’s a boxed u<strong>in</strong>t or long, forexample, or not a boxed value at all) an InvalidCastException is thrown. 10That’s it, really—box<strong>in</strong>g and unbox<strong>in</strong>g <strong>in</strong> a nutshell. The only rema<strong>in</strong><strong>in</strong>g problemis know<strong>in</strong>g when box<strong>in</strong>g and unbox<strong>in</strong>g occur. Unbox<strong>in</strong>g is usually obvious, because thecast is present <strong>in</strong> the code. Box<strong>in</strong>g can be more subtle. We’ve seen the simple version,but it can also occur if you call the ToStr<strong>in</strong>g, Equals, or GetHashCode methods on thevalue of a type that doesn’t override them, or if you use the value as an <strong>in</strong>terface expression—assign<strong>in</strong>git to a variable whose type is an <strong>in</strong>terface type or pass<strong>in</strong>g it as a parameterwith an <strong>in</strong>terface type. For example, the statement IComparable x = 5; would boxthe number 5.It’s worth be<strong>in</strong>g aware of box<strong>in</strong>g and unbox<strong>in</strong>g because of the potential performancepenalty <strong>in</strong>volved. A s<strong>in</strong>gle box or unbox operation is very cheap, but if you10 There are corner cases where the type doesn’t have to be exactly right, mostly to do with enums. These are sofiddly that even the <strong>C#</strong> language specification hasn’t got it quite right yet!Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!