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.

52 CHAPTER 2 Core foundations: build<strong>in</strong>g on <strong>C#</strong> 1the 32-bit or 64-bit CLR) rather than copy<strong>in</strong>g all the data. Imag<strong>in</strong>e if ArrayList weresomehow a “pure” value type, and pass<strong>in</strong>g an ArrayList expression to a method<strong>in</strong>volved copy<strong>in</strong>g all its data!MYTH #2: “REFERENCE TYPES LIVE ON THE HEAP, VALUE TYPES LIVE ON THE STACK”This one is often caused just by laz<strong>in</strong>ess on the part of the person repeat<strong>in</strong>g it. Thefirst part is correct—an <strong>in</strong>stance of a reference type is always created on the heap. It’sthe second part that is problematic. As we’ve already noted, a variable’s value liveswherever it’s declared—so if you have a class with an <strong>in</strong>stance variable of type <strong>in</strong>t, thatvariable’s value for any given object will always be where the rest of the data for theobject is—on the heap. Only local variables (variables declared with<strong>in</strong> methods) andmethod parameters live on the stack.NOTE Are these concepts relevant now? It’s arguable that if you’re writ<strong>in</strong>g managedcode, you should let the runtime worry about how memory is bestused. Indeed, the language specification makes no guarantees aboutwhat lives where; a future runtime may be able to create some objects onthe stack if it knows it could get away with it, or the <strong>C#</strong> compiler couldgenerate code that hardly uses the stack at all.The next myth is usually just a term<strong>in</strong>ology issue.MYTH #3: “OBJECTS ARE PASSED BY REFERENCE IN <strong>C#</strong> BY DEFAULT”This is probably the most widely propagated myth. Aga<strong>in</strong>, the people who make thisclaim often (though not always) know what the actual <strong>C#</strong> behavior is, but they don’tknow what “pass by reference” really means. Unfortunately, this leads to people whodo know what it means gett<strong>in</strong>g confused. The formal def<strong>in</strong>ition of “pass by reference”is relatively complicated, <strong>in</strong>volv<strong>in</strong>g l-values and similar computer science term<strong>in</strong>ology,but the important th<strong>in</strong>g is that if you pass a variable by reference, the method you’recall<strong>in</strong>g can change the value of the caller’s variable by chang<strong>in</strong>g its parameter value. Nowremember that the value of a reference type variable is the reference, not the objectitself. You can change the contents of the object that a parameter refers to without theparameter itself be<strong>in</strong>g passed by reference. For <strong>in</strong>stance, the follow<strong>in</strong>g methodchanges the contents of the Str<strong>in</strong>gBuilder object <strong>in</strong> question, but the caller’s expressionwill still refer to the same object as before:void AppendHello (Str<strong>in</strong>gBuilder builder){builder.Append("hello");}When this method is called, the parameter value (a reference to a Str<strong>in</strong>gBuilder) ispassed by value. If I were to change the value of the builder variable with<strong>in</strong> themethod—for example with the statement builder = null;—that change wouldn’t beseen by the caller, contrary to the myth.It’s <strong>in</strong>terest<strong>in</strong>g to note that not only is the “by reference” bit of the myth <strong>in</strong>accurate,but so is the “objects are passed” bit. Objects themselves are never passed, eitherby reference or by value. When a reference type is <strong>in</strong>volved, either the variable isLicensed to Rhona Hadida

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

Saved successfully!

Ooh no, something went wrong!