Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Teaching References in C#

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.
like image 270
Nicholas Armstrong Avatar asked Oct 29 '09 20:10

Nicholas Armstrong


People also ask

Can you use references in C?

No, it doesn't. It has pointers, but they're not quite the same thing. For more details about the differences between pointers and references, see this SO question.

What is a reference in C language?

A reference parameter "refers" to the original data in the calling function. Thus any changes made to the parameter are ALSO MADE TO THE ORIGINAL variable. There are two ways to make a pass by reference parameter: ARRAYS. Arrays are always pass by reference in C.

How do I start teaching C programming?

Get started with C. Official C documentation - Might be hard to follow and understand for beginners. Visit official C Programming documentation. Write a lot of C programming code - The only way you can learn programming is by writing a lot of code.

What are pointers and reference in C?

To store a reference is to store an address in the memory of a variable. A pointer is a variable itself and has a value whereas a reference only has a variable that it is referencing.


1 Answers

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.

like image 161
Joel Martinez Avatar answered Nov 15 '22 02:11

Joel Martinez