是列表吗?一个指针?

发布于 2024-12-10 13:32:32 字数 593 浏览 1 评论 0原文

我注意到 List 的行为与其他简单对象(例如 String)不同。这个问题看起来很新手,但这确实让我震惊,因为我认为 List 是简单的对象。

以下面的代码为例:

List<String> ls1 = new List<String>();
ls1.Add("a");
List<String> ls2 = ls1;
ls1.Add("b");

最后,ls1 将等于 {"a", "b"}ls2 也将等于。这与此代码的行为确实不同:

String s1 = "a";
String s2 = s1;
s1 = "b";

其中 s1 最后等于 bs2 等于 a< /代码>。

这意味着 List 实际上是一个指针,对吗?

I noticed that the behavior of List<T> is different from an other simple object, say String for example. The question can seem newbie but that really struck me cause I thought that List<T> were simple objects.

Take for instance the following code:

List<String> ls1 = new List<String>();
ls1.Add("a");
List<String> ls2 = ls1;
ls1.Add("b");

At the end, ls1 will be equal to {"a", "b"} and so will ls2. This is really different from the behavior of this code:

String s1 = "a";
String s2 = s1;
s1 = "b";

Where s1 is at the end equal to b and s2 equal to a.

That means that List<T> is in fact a pointer right?

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

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

发布评论

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

评论(5

宣告ˉ结束 2024-12-17 13:32:32

List 一个引用类型,所以它的行为就像一个指针。

String 也是一种引用类型,但字符串是不可变的,并且在某些情况下它们的行为类似于值类型(与引用类型对比),因此您会感到困惑。

这里有一个很好的解释为什么字符串以这种方式工作: 在 C# 中,为什么 String 是一种行为类似于值类型的引用类型?

List<T> a reference type, so yes it behaves like a pointer.

String is also a reference type, but strings are immutable and they behave like value types (contrast with reference types) in some cases hence your confusion here.

There is a good explanation of why string work this way here: In C#, why is String a reference type that behaves like a value type?

夕色琉璃 2024-12-17 13:32:32

s1 = "b" 行实际上为 s1 分配了一个新引用。 s1s2 现在引用两个不同的对象。

ls1 引用的 List 对象的更改通过对该对象的所有引用(包括 ls2)可见。当您创建 ls2 = ls1 时,您基本上是在说 ls1ls2 引用同一个对象。通过引用变量 ls1 对对象所做的更改可以通过引用变量 ls2 看到。

The line s1 = "b" actually assigns a new reference to s1. s1 and s2 now refer to two different objects.

The changes to the List<string> object referred to by ls1 are visible through all references to that object, including ls2. When you make ls2 = ls1 you are basically saying that both ls1 and ls2 refer to the same object. Changes to the object via the reference variable ls1 are visible via the reference variable ls2.

眼泪也成诗 2024-12-17 13:32:32

在 C# 中,它们称为引用,而不是指针。可能它们是同一件事,只是减去了不能将它们转换为整数来打印它们的事实,并减去了被禁止的指针算术。从技术上讲,它们是“不透明的”,所以你不(不应该)知道它们是如何工作的。显然,如果您使用托管 C++,这种不透明性就会被打破:-)

In C# they are called references, not pointers. Probably they are the same thing minus the fact that you can't cast them to an integer to print them and minus the pointer arithmetic that is forbidden. Technically they are "opaque", so you don't (shouldn't) know how they work. Clearly this opaqueness is broken if you use Managed C++ :-)

嘿哥们儿 2024-12-17 13:32:32

C# 中的每个对象都是一个引用。像 int、string、char 这样的基元不是引用。

Every object in C# is a reference. Primitives like int, string, char, are not references.

雨落□心尘 2024-12-17 13:32:32

String、int、float 都是值类型。因此,当您说

String s1 = "a";
String s2 = s1;
s1 = "b";

当 s2 初始化时,s1 的值将复制到分配给 s2 的空间中。因此,如果您正在窥探内存,您可以在内存中的两个不同位置(分配给 s1 的位置和分配给 s2 的内存)找到“a”的十六进制表示形式。但是,如果您尝试对引用类型(例如 List)执行相同的操作,那么当您说这样的内容是按引用传递时会发生什么:

List<String> ls1 = new List<String>();
ls1.Add("a");
List<String> ls2 = ls1;
ls1.Add("b");

具有与在堆上分配的 List 相对应的对象。查找该对象的地址驻留在为局部变量 s1 和 s2 分配的空间上,而不是驻留在该空间中的实际值。原因是对象(List)可能很大,并且在程序的生命周期中可能存在很长时间,并且为这样的对象从堆栈中分配内存将是昂贵的。
我建议您阅读这个问题线程,以了解有关 CLR 如何真正解释值类型和引用类型的更多信息

String, int, float are all value types. So, when you say

String s1 = "a";
String s2 = s1;
s1 = "b";

When s2 is initialised, the value of s1 is copied into the space allocated to s2. So, if you were snooping on the memory, you could find the hex representation of "a" at two distinct locations in the memory(the location allocated to s1 and the memory allocated to s2). But if you try to do the same with a reference type, like List, what happens when you say something like this is a pass by reference:

List<String> ls1 = new List<String>();
ls1.Add("a");
List<String> ls2 = ls1;
ls1.Add("b");

is that have the object corresponding to the List allocated on the heap. The address of where to find this object resides on the space allocated for the local variables s1 and s2 instead of the actual value residing in that space. The reason is that the object(List) might be a large one and potentially long lived through the life of the program and it would be expensive to allocate memory off the stack for such an object.
I recommend you read this question thread to understand more about how value types and reference types are really interpreted by the CLR

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文