列表中的实例由新的实例修改 [英] An instance in a list is modified by a new one

查看:75
本文介绍了列表中的实例由新的实例修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我有点想法,所以我需要你的帮助。

很难解释情况,但我会尽我所能。



我创建了一个程序来创建某些类型的汽车并将它们添加到车库。

我代表带有列表的车库。我将值放到一个实例并将其添加到列表中,然后我意识到当我开始向新实例添加值时,它会更改列表中实例的值。



我很困惑请帮忙。



我尝试过:



真的不知道该尝试什么,我真的不知道它是怎么回事

Hi all ! I'm kind of out of ideas so I need your help in this one.
it's hard to explain the situation but I'l try my best.

I have created a program that creates cars of some types and adds them to a Garage.
I represent the garage with a list. I put values to one instance and add it to the list, then I realize that when I start to add values to a new instance, it changes the values of the instance inside the list.

I'm confused Please help.

What I have tried:

don't really know what to try, I don't really know how it happened

推荐答案

这是正确的,列表中的元素只是实际汽车实例的引用(指针),这称为传递参考类型参数(C#编程指南) ) [ ^ ]。通常,我们会在储物柜中保留一个实际的工件(按值)。但是,为了通过引用传递,您可以在该锁定器中的一张纸上保存有关该文章的实际位置(地址)的信息,该锁定器是C编程中的所谓指针。为了更好地掌握这个概念,请阅读指针 [ ^ ]。
That is correct, the elements in the list are just references (pointers) to the actual car instances, this is called Passing Reference-Type Parameters (C# Programming Guide)[^]. Usually, we keep an actual artifact (by value) in a locker. For passing by reference, however, you keep information about the actual location (address) of that article on a piece of paper in that locker, this locker is the so-called pointer in C programming. To better grasp the concept, read out pointer[^].


Quote:

我代表带有列表的车库。我将值放到一个实例并将其添加到列表中,然后我意识到当我开始向新实例添加值时,它会更改列表中实例的值。

I represent the garage with a list. I put values to one instance and add it to the list, then I realize that when I start to add values to a new instance, it changes the values of the instance inside the list.



即使没有代码,也可以说你认为2个不同事物的2个实例实际上是指向同一个实例的2个指针,因此就是问题。



沿随着对编程技术的深入学习,你应该学习调试器。



当你不理解你的代码在做什么或为什么它做它做的时候,答案是调试器

使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期的效果时,你就会接近一个错误。


Even without code, one can say is that what you think is 2 instances of 2 different things is in fact 2 pointers to same instance, thus the problem.

Along with deeper learning of programming techniques, you should learn debugger.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


这篇关于列表中的实例由新的实例修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆