views:

222

answers:

7

In a couple of weeks, I'll be teaching a class of first-year engineers the salient points of references in C# as part of their first-year programming course. Most of them have never programmed before, and had enough trouble learning objects, so teaching references is going to be an uphill battle. I plan to have lots of examples available for the students to go through on their own, but just showing a bunch of examples tends to be pretty overwhelming if the the underlying concept doesn't 'click'.

So I'll put the question out to the SO community: what's the best way you've seen references taught? What made it 'click' for you? Is there any reference-related material that I'm missing?

My tentative lesson plan is:

  1. What is a reference (using an argument like Eric Lippert's)
  2. References and the Garbage Collector
  3. Reference Types and Value Types
  4. Immutable Types
  5. Passing by Reference versus Passing by Value (and all of the subtleties of object references being passed by value)
  6. A handful of nasty examples that produce unexpected results if you don't understand 1-5.
+1  A: 

I found this article really useful for explaning parameter passing in C#. The article also does a good job explaining value and reference types in general terms.

It's more of a visual representation which helped me a lot.

Jay Riggs
+4  A: 

One way that I've heard it explained is to use a cell phone or walkie-talkie. You (the instructor) hold one end and declare that you are an object instance. You stay in one place (ie. the heap) while the students pass the other end (which is on speaker phone if it's a cell phone) around the classroom.

They can interact with you through the "reference" they have to you, but they don't really have "you" in their possession.

Joel Martinez
And by extension, they *could* change the reference by calling someone else, and change the object instance in question...
Rowland Shaw
+2  A: 

I like the URL analogy that describes the differences between Reference and Value types. You can pass around a URL as a reference to some content. You can modify that URL without modifying that content. You can also get to the content via the URL to perhaps modify the content.

This is a useful reference:

 http://www.yoda.arachsys.com/csharp/parameters.html
Josh Smeaton
+1  A: 

Pictures and diagrams.

People form mental images of the concepts they're learning, and a visual representation of references and their relation to their associated objects is a good way to start. Likewise, visualizing object as containing member variables (which includes references to other objects) and member methods, a la UML diagrams, is very helpful.

Later, you can delve into the details of how references and primitive types are actually implemented, if you feel the need to do so. But delay these discussions as long as possible, as people can get bogged down in trying to pair abstract concepts to the representational details, which distracts from learning the abstract concepts.

Loadmaster
+1  A: 

When I was learning VB6, references actually confused me a bit. Then I tried learning C++, and after dealing with pointers, references made perfect sense to me. Understanding it from a what-is-actually-happening perspective was easier to me than understanding it from an oo-concepts perspective. Maybe you can go over the under-the-hood stuff in your lesson.

Snarfblam
+2  A: 

Binky! (or @ http://cslibrary.stanford.edu/104/)

mrnye
+2  A: 

Try to explain references with figures, as pure text sometimes don't get through to most people. Many resources and books on the topic, do try to explain through figures as it is difficult to relate allocation through verbal communication alone (this is mostly an issue of attention span for most people).

At least try to point out how objects relate to each other, a simple example would be a simple reference.

Given:

class A {
    B b = new B();
}

class B {
   int mine = 1;
}

When instantiating class A as object a from some context the following figure will illustrate how it will all look in the heap. The point of the illustration is to show how the different objects relate to each other and have a mental model for how the heap works.

         +-A-----+
a: *---->|       |
         |       |   +-B--------+
         | b: *--+-->|          |
         |       |   | mine: 1  |
         +-------+   |          |
                     +----------+

Also try to explain the difference between heap and stack allocation. Calling a method with parameters. Simple example would be something like this:

Given the following method:

public void doSomething(B b) {
   int doMine = b.mine + 1;
}

When calling doSomething and letting it do it's stuff, at the end doSomething's stack will look something like below. The point showing that objects do not directly reside inside a stack, but it is just referred to an object in the heap and objects are shared through references.

whoever called doSomething *
                           |
                           v
+-doSomething-+   +-B--------+
| b: *--------+-->|          |
|-------------|   | mine: 1  |
| doMine: 2   |   +----------+
+-------------+

Another illustrative example would be illustrating an array which is an object, and a multidimensional array contains an array of arrays.

Spoike