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.

50 CHAPTER 2 Core foundations: build<strong>in</strong>g on <strong>C#</strong> 1Now that we’ve got the basic idea of what reference types and value types are about,we’ll look at a few of the most important details.2.3.2 Value and reference type fundamentalsThe key concept to grasp when it comes to value types and reference types is what thevalue of a particular expression is. To keep th<strong>in</strong>gs concrete, I’ll use variables as themost common examples of expressions—but the same th<strong>in</strong>g applies to properties,method calls, <strong>in</strong>dexers, and other expressions.As we discussed <strong>in</strong> section 2.2.1, most expressions have types associatedwith them. The value of a value type expression is the value, pla<strong>in</strong> andsimple. For <strong>in</strong>stance, the value of the expression “2+3” is just 5. The valueof a reference type expression, however, is a reference. It’s not the objectthat the reference refers to. So, the value of the expression Str<strong>in</strong>g.Empty is not an empty str<strong>in</strong>g—it’s a reference to an empty str<strong>in</strong>g. In everydaydiscussions and even <strong>in</strong> documentation we tend to blur this dist<strong>in</strong>ction.For <strong>in</strong>stance, we might describe Str<strong>in</strong>g.Concat as return<strong>in</strong>g “astr<strong>in</strong>g that is the concatenation of all the parameters.” Us<strong>in</strong>g very precise term<strong>in</strong>ologyhere would be time-consum<strong>in</strong>g and distract<strong>in</strong>g, and there’s no problem so long aseveryone <strong>in</strong>volved understands that it’s only a reference that is returned.To demonstrate this further, consider a Po<strong>in</strong>t type that stores two <strong>in</strong>tegers, x and y.It could have a constructor that takes the two values. Now, this type could be implementedas either a struct or a class. Figure 2.3 shows the result of execut<strong>in</strong>g the follow<strong>in</strong>gl<strong>in</strong>es of code:Key po<strong>in</strong>t!Po<strong>in</strong>t p1 = new Po<strong>in</strong>t(10, 20);Po<strong>in</strong>t p2 = p1;The left side of figure 2.3 <strong>in</strong>dicates thevalues <strong>in</strong>volved when Po<strong>in</strong>t is a class (areference type), and the right side showsthe situation when Po<strong>in</strong>t is a struct (avalue type).In both cases, p1 and p2 have thesame value after the assignment. However,<strong>in</strong> the case where Po<strong>in</strong>t is a referencetype, that value is a reference: bothp1 and p2 refer to the same object. WhenPo<strong>in</strong>t is a value type, the value of p1 is thewhole of the data for a po<strong>in</strong>t—the x andWhen Po<strong>in</strong>t isa value typey values. Assign<strong>in</strong>g the value of p1 to p2 copies all of that data.The values of variables are stored wherever they are declared. Local variable valuesare always stored on the stack, 9 and <strong>in</strong>stance variable values are always stored whereverp1refp2refx10When Po<strong>in</strong>t is areference type20x10x10Figure 2.3 Compar<strong>in</strong>g value type andreference type behaviors, particularlywith regard to assignmentyp1p1y20y209This is only totally true for <strong>C#</strong> 1. We’ll see later that local variables can end up on the heap <strong>in</strong> certa<strong>in</strong> situations<strong>in</strong> <strong>C#</strong> 2 and 3.Licensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!