C# 教学参考

发布于 2024-08-09 13:47:48 字数 649 浏览 10 评论 0原文

几周后,我将向一班一年级工程师讲授 C# 中的要点,作为他们一年级编程课程的一部分。他们中的大多数人以前从未编程过,并且在学习对象时遇到了足够的困难,因此教授参考文献将是一场艰苦的战斗。我计划提供大量示例供学生自己完成,但如果基本概念没有“点击”,仅仅展示一堆示例往往会让人不知所措。

因此,我将向 SO 社区提出这个问题:您见过的参考文献教学的最佳方式是什么?是什么让你“点击”了它?我是否缺少任何参考相关材料?

我的暂定课程计划是:

  1. 什么是参考(使用 像 Eric Lippert 的论点
  2. 引用和垃圾收集器
  3. 引用类型和值类型
  4. 不可变类型
  5. 通过引用传递与通过值传递(以及按值传递对象引用的所有微妙之处
  6. 一些令人讨厌的示例,如果您不理解 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:

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

星光不落少年眉 2024-08-16 13:47:48

我听到的一种解释方法是使用手机或对讲机。你(指导者)握住一端并声明你是一个对象实例。你呆在一个地方(即一堆),而学生则在教室周围经过另一端(如果是手机,则为免提电话)。

他们可以通过对你的“参考”与你互动,但他们并不真正拥有“你”。

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.

思念绕指尖 2024-08-16 13:47:48

我喜欢用 URL 类比来描述引用类型和值类型之间的差异。您可以传递 URL 作为对某些内容的引用。您可以修改该 URL,而不修改该内容。您还可以通过 URL 访问内容,或许可以修改内容。

这是一个有用的参考:

 http://www.yoda.arachsys.com/csharp/parameters.html

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
清眉祭 2024-08-16 13:47:48

尝试用数字来解释参考文献,因为纯文本有时无法让大多数人理解。关于该主题的许多资源和书籍确实尝试通过数字进行解释,因为仅通过口头交流很难将分配联系起来(这主要是大多数人注意力持续时间的问题)。

至少尝试指出对象如何相互关联,一个简单的例子就是一个简单的引用。

假设:

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

class B {
   int mine = 1;
}

当从某个上下文将类 A 实例化为对象 a 时,下图将说明它在堆中的外观。该图的目的是展示不同对象如何相互关联,并对堆如何工作有一个心理模型。

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

还尝试解释堆和堆栈分配之间的区别。调用带参数的方法。简单的例子是这样的:

给定以下方法:

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

当调用doSomething并让它做它的事情时,最后doSomething的堆栈将如下所示。这表明对象并不直接驻留在堆栈中,而只是引用堆中的对象,并且对象通过引用共享。

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

另一个说明性示例是说明作为对象的数组,并且多维数组包含数组的数组。

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.

叹倦 2024-08-16 13:47:48

我发现这篇文章对于解释参数传递非常有用在 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.

寒冷纷飞旳雪 2024-08-16 13:47:48

图片和图表。

人们会在脑海中形成他们正在学习的概念的图像,而参考的视觉表示及​​其与相关对象的关系是一个很好的开始。同样,将对象可视化为包含成员变量(包括对其他对象的引用)和成员方法(如 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.

丶情人眼里出诗心の 2024-08-16 13:47:48

当我学习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.

私藏温柔 2024-08-16 13:47:48

我建议尽量减少使用“引用”一词,因为它可以在 .net 中用来指代两个截然不同的事物:类类型存储位置的内容,以及使用“ref”限定符传递的参数。前者使用术语“对象引用”,后者使用“ref 参数”。

在描述什么是“对象引用”时,我建议使用术语“对象 ID”。对象 ID 有一些与“地址”不同的特征:

  1. 使用对象 ID 不能做很多事情。我们可以测试一个是否为空,检查其中两个是否相等,将一个复制到合适类型的存储位置,或者查找一个引用的对象并要求它做某事。大多数对类类型值或变量执行某些操作的请求实际上是对所引用的对象执行某些操作的请求。请注意,我们无法像处理地址那样通过操作一个对象的 ID 来获取另一个对象的 ID。
  2. 虽然系统必须有一种将对象 ID 转换为地址的方法,但不能保证它将使用任何特定的方法来执行此操作。也不保证与任何对象 ID 关联的位模式不会自发更改;所保证的是,如果位模式发生更改,新模式将引用与旧模式相同的对象。
  3. 系统会跟踪存储对象 ID 的每个位置。只要对象 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":

  1. One can't do very many things with object ID's. One can test whether one is blank, check whether two of them are equal, copy one to a storage location of suitable type, or look up the object referred to by one and ask it to do something. Most requests to do something with a class-type value or variable are really requests to do something with the referred-to object. Note that one cannot manipulate an ID of one object in such a way as to get the ID of another, as one can do with addresses.
  2. While the system must have a means of converting object ID's to addresses, there is no guarantee that it will use any particular means of doing so. Nor is there any guarantee that the bit pattern associated with any object ID won't spontaneously change; all that is guaranteed is that if the bit pattern changes, the new pattern will refer to the same object as the old.
  3. The system keeps track of every place that object ID's are stored. As long as any copy of an Object ID exists, that object ID will never refer to anything other than the object instance for which it was created. By contrast, in general, systems that use addresses for things do not track every single place where an address might be copied. It's possible that an object might cease to exist while somebody still has a copy of its address, and some new object might be created with the same address.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文