C#输出对象的引用

时间:2022-02-02 16:54:08

in what way can I output the reference of an object in memory. Like:

以什么方式可以输出内存中对象的引用。喜欢:

Console.WriteLine("Object at Memory Location " + object.Reference);

Console.WriteLine(“内存位置的对象”+ object.Reference);

thanks in advance, I need this to do some debugging.

提前谢谢,我需要这个做一些调试。

3 个解决方案

#1


For a managed object you can't and for good reason. It can change location during any garbage collection.

对于托管对象,您不能并且有充分理由。它可以在任何垃圾收集期间更改位置。

#2


Reference are not memory location. Check this blog: Fabulous Adventures In Coding

引用不是内存位置。查看此博客:编码中的精彩冒险

#3


Every so often you may see a .NET tutorial mention "pointers" and someone get mad in the comments saying that "references" and not "pointers" should be discussed. This is why. The distinction is usually trivial, especially when teaching higher concepts but at times like this, it's very non-trivial.

每隔一段时间你就会看到一个.NET教程提到“指针”,有人会在评论中生气,说应该讨论“引用”而不是“指针”。这就是为什么。区别通常是微不足道的,特别是在教授更高概念时,但有时这样,这是非常重要的。

The actual pointers are handled by .NET and .NET often changes the locations of objects in memory and thus changes/updates the pointers. You and I deal with the references and our reference doesn't need to change when the pointers do because .NET handles this mapping for us. So although there are always pointers involved, even if we do get what the pointer is pointing to at the moment, this doesn't necessarily mean that you're finding out where it will continue to be.

实际的指针由.NET处理,.NET通常会更改内存中对象的位置,从而更改/更新指针。你和我处理引用,我们的引用不需要在指针执行时更改,因为.NET为我们处理这个映射。因此,尽管总是涉及指针,即使我们确实得到了指针指向的内容,但这并不一定意味着您将找到它将继续存在的位置。

That said, you can still use the below, but I don't think it will achieve what you desire:

也就是说,你仍然可以使用下面的内容,但我认为它不会达到你想要的效果:

int* myIntPointer = &myInt;

#1


For a managed object you can't and for good reason. It can change location during any garbage collection.

对于托管对象,您不能并且有充分理由。它可以在任何垃圾收集期间更改位置。

#2


Reference are not memory location. Check this blog: Fabulous Adventures In Coding

引用不是内存位置。查看此博客:编码中的精彩冒险

#3


Every so often you may see a .NET tutorial mention "pointers" and someone get mad in the comments saying that "references" and not "pointers" should be discussed. This is why. The distinction is usually trivial, especially when teaching higher concepts but at times like this, it's very non-trivial.

每隔一段时间你就会看到一个.NET教程提到“指针”,有人会在评论中生气,说应该讨论“引用”而不是“指针”。这就是为什么。区别通常是微不足道的,特别是在教授更高概念时,但有时这样,这是非常重要的。

The actual pointers are handled by .NET and .NET often changes the locations of objects in memory and thus changes/updates the pointers. You and I deal with the references and our reference doesn't need to change when the pointers do because .NET handles this mapping for us. So although there are always pointers involved, even if we do get what the pointer is pointing to at the moment, this doesn't necessarily mean that you're finding out where it will continue to be.

实际的指针由.NET处理,.NET通常会更改内存中对象的位置,从而更改/更新指针。你和我处理引用,我们的引用不需要在指针执行时更改,因为.NET为我们处理这个映射。因此,尽管总是涉及指针,即使我们确实得到了指针指向的内容,但这并不一定意味着您将找到它将继续存在的位置。

That said, you can still use the below, but I don't think it will achieve what you desire:

也就是说,你仍然可以使用下面的内容,但我认为它不会达到你想要的效果:

int* myIntPointer = &myInt;