C# 教学参考
几周后,我将向一班一年级工程师讲授 C# 中的要点,作为他们一年级编程课程的一部分。他们中的大多数人以前从未编程过,并且在学习对象时遇到了足够的困难,因此教授参考文献将是一场艰苦的战斗。我计划提供大量示例供学生自己完成,但如果基本概念没有“点击”,仅仅展示一堆示例往往会让人不知所措。
因此,我将向 SO 社区提出这个问题:您见过的参考文献教学的最佳方式是什么?是什么让你“点击”了它?我是否缺少任何参考相关材料?
我的暂定课程计划是:
- 什么是参考(使用 像 Eric Lippert 的论点)
- 引用和垃圾收集器
- 引用类型和值类型
- 不可变类型
- 通过引用传递与通过值传递(以及按值传递对象引用的所有微妙之处)
- 一些令人讨厌的示例,如果您不理解 1-5,它们会产生意外结果。
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:
- What is a reference (using an argument like Eric Lippert's)
- References and the Garbage Collector
- Reference Types and Value Types
- Immutable Types
- Passing by Reference versus Passing by Value (and all of the subtleties of object references being passed by value)
- A handful of nasty examples that produce unexpected results if you don't understand 1-5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我听到的一种解释方法是使用手机或对讲机。你(指导者)握住一端并声明你是一个对象实例。你呆在一个地方(即一堆),而学生则在教室周围经过另一端(如果是手机,则为免提电话)。
他们可以通过对你的“参考”与你互动,但他们并不真正拥有“你”。
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.
Binky!(或@http://cslibrary.stanford.edu/104/)
Binky! (or @ http://cslibrary.stanford.edu/104/)
我喜欢用 URL 类比来描述引用类型和值类型之间的差异。您可以传递 URL 作为对某些内容的引用。您可以修改该 URL,而不修改该内容。您还可以通过 URL 访问内容,或许可以修改内容。
这是一个有用的参考:
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:
尝试用数字来解释参考文献,因为纯文本有时无法让大多数人理解。关于该主题的许多资源和书籍确实尝试通过数字进行解释,因为仅通过口头交流很难将分配联系起来(这主要是大多数人注意力持续时间的问题)。
至少尝试指出对象如何相互关联,一个简单的例子就是一个简单的引用。
假设:
当从某个上下文将类
A
实例化为对象a
时,下图将说明它在堆中的外观。该图的目的是展示不同对象如何相互关联,并对堆如何工作有一个心理模型。还尝试解释堆和堆栈分配之间的区别。调用带参数的方法。简单的例子是这样的:
给定以下方法:
当调用
doSomething
并让它做它的事情时,最后doSomething
的堆栈将如下所示。这表明对象并不直接驻留在堆栈中,而只是引用堆中的对象,并且对象通过引用共享。另一个说明性示例是说明作为对象的数组,并且多维数组包含数组的数组。
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:
When instantiating class
A
as objecta
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.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:
When calling
doSomething
and letting it do it's stuff, at the enddoSomething
'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.Another illustrative example would be illustrating an array which is an object, and a multidimensional array contains an array of arrays.
我发现这篇文章对于解释参数传递非常有用在 C# 中。这篇文章还很好地用一般术语解释了值和引用类型。
它更多的是一种视觉表现,对我帮助很大。
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.
图片和图表。
人们会在脑海中形成他们正在学习的概念的图像,而参考的视觉表示及其与相关对象的关系是一个很好的开始。同样,将对象可视化为包含成员变量(包括对其他对象的引用)和成员方法(如 UML 图)非常有帮助。
稍后,如果您觉得有必要,您可以深入研究引用和基本类型实际实现方式的细节。但请尽可能推迟这些讨论,因为人们可能会陷入尝试将抽象概念与表征细节配对的困境,这会分散学习抽象概念的注意力。
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.
当我学习VB6时,参考文献实际上让我有点困惑。然后我尝试学习 C++,在处理完指针之后,引用对我来说非常有意义。对我来说,从实际发生的事情的角度理解它比从面向对象概念的角度理解它更容易。也许您可以在课程中回顾一下幕后的内容。
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.
我建议尽量减少使用“引用”一词,因为它可以在 .net 中用来指代两个截然不同的事物:类类型存储位置的内容,以及使用“ref”限定符传递的参数。前者使用术语“对象引用”,后者使用“ref 参数”。
在描述什么是“对象引用”时,我建议使用术语“对象 ID”。对象 ID 有一些与“地址”不同的特征:
I would suggest minimizing one's use of the bare term "reference" altogether, since it can be used in .net to refer to two very different things: the content of class-type storage locations, and parameters passed with a "ref" qualifier. Use the term "object reference" for the former, and "ref parameter" for the latter.
In describing what an "object reference" is, I would suggest using the term "object ID". Object ID's have a few things that make them different from "addresses":