Parameter Passing in C#: 1. Value Types

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Parameter passing in C#

So there I was, merrily browsing the Internet when I came across this fantastic site on
topics such as:

Implementing a singleton pattern in C#,

Type initializers,

Static constructors,

Delegates and events,

And as my title suggests: Parameter passing in C#.


In short, all those things you rarely need to know to get your job done, but that separate
mediocre developers from good ones. The site is by Jon Skeet and the articles are
informative, well researched, well explained, well written. Here's the C# part of his site:
http://www.yoda.arachsys.com/csharp/
The article that caught my attention made sense to me, but being a very visual person I
couldn't help but think that some pictures could really help illustrate the points. So without
further ado, I illustrated the article. You probably don't need to read the article to
understand this post - but you should:
http://www.yoda.arachsys.com/csharp/parameters.html.
Note: you can click the images to get a clearer view.
1. Value Types
Notice that the values live inside the box which will not be the case for reference types.
Also, the assignment operation copies the value inside of the box, this is important to
compare with reference types.

Quiz: What is the result of the WriteLine statement?


Answer: 5
2. Reference Types
Variables that hold reference types actually hold a reference to a location in memory (on
the heap). So assignment operations copy the address. Notice this is still consistent with
diagram #1, the copy operation copies the value inside of the box.

Quiz: What is the result of the WriteLine statement?


Answer: hello world
3. Immutable Reference Types
Immutable reference types like strings behave just like regular reference types except
they don't provide a way to change their value.

Quiz: What is the result of the WriteLine statement?


Answer: hello
4. Value Types Passed by Value
Passing a variable to a function by value is equivilant to instantiating a new variable and
assigning it to the first (well, ignoring scope issues and such). Notice that the diagram
below is nearly identical to diagram #1.

Quiz: What is the result of the WriteLine statement?


Answer: 5, same as #1
5. Reference Types Passed by Value
In #4 I said passing a variable to a function by value is equivilant to instantiating a new
variable and assigning it to the first. Is that still true of reference types? Yup. And did you
notice there's an implicit assignment statement when passing by value? As you'll see
shortly there won't be when passing by reference.

Quiz: What is the result of the WriteLine statement?


Answer: hello world
6. Value Types Passed by Reference
Passing by reference doesn't involve an implicity copy, instead it instantiates the inner
variable to the address in memory of the outer variable. Then all references to the inner
variable are implicitly dereferenced for you and voila, magically you're changing the value
of the outer variable.

Quiz: What is the result of the WriteLine statement?


Answer: 10, and notice how different the diagram and results are than #1 and #4.
7. Reference Types Passed by Reference
Really this is no different than value types passed by reference (#6), except calling
sb.Append() from an inner variable is dereferenced once to get to the outer variable and
again because the outer variable is itself a pointer.

By the way, when you get to the section in Jon's article called:
"Sidenote: what is the difference between passing a value object by reference and a
reference object by value?"
Please read it carefully, it's an extremely good point. It can be sumed up by comparing
the final assignment statement above (Reference Types Passed by Reference) to the
final assignment statement in in diagram #5 (Reference Types Passed by Value). It's a
subtle, but important difference.
Oh and the quiz, what is the value of the Console.WriteLine in #7?
Answer: NullReferenceExceptinon Object reference not set to an instance of an object
Still confused? Then I didn't do my job right, since this is the point in the article when I
thought pictures would help. So please post your thoughts whether it makes sense or
not.
- Lee

You might also like