Guid 被视为值类型还是引用类型?
指南是使用 new 关键字创建的,这让我认为它是引用类型。
这是正确的吗?
Guid uid = new Guid();
Guid 是否存储在堆上?
Guids are created using the new
keyword which makes me think it's a reference type.
Is this correct?
Guid uid = new Guid();
Are Guids stored on the heap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Guid 是一种值类型。
请参阅 MSDN。请注意,Guid 是一个结构。所有结构都是值类型。
Guid is a Value Type.
See MSDN. Note that Guid is a struct. All structs are Value Types.
你可以自己看看Guid的定义:
或者你可以这样测试:
结构也可以有构造函数,例如
new DateTime(2012, 12, 23)
。You can see the definition of a Guid yourself:
Or you can test it like this:
Structs can have constructors too, for example
new DateTime(2012, 12, 23)
.别再这么想了。值类型也可以有构造函数。虽然很奇怪,但说
这与将零分配给 x 是完全合法的。
没有。
是的。指南也存储在堆栈中。
请注意,下面的分析假设 CLI 的实现是在 Windows 上运行的 Microsoft“桌面”或“Silverlight”CLR。我不知道其他版本的 CLI 的作用是什么,它们在 Mac 上的作用等等。如果您需要知道特定的内存块是否存储在其他实现中的堆栈上,则必须询问这些实现方面的专家。
Guid 在以下情况下存储在堆栈中:
(1) 当 Guid 是正在进行的计算的“临时”结果或用作方法的参数时。例如,如果您有一个方法调用 M(new Guid()),则在堆栈上分配新 Guid 的临时存储空间。
(2) 当 Guid 是局部变量时,该局部变量 (a) 不在迭代器块中,(b) 不是匿名方法或 lambda 表达式的封闭外部变量。
在所有其他情况下,Guid 不存储在堆栈中。当 Guid 是引用类型的字段、数组的元素、匿名方法或 lambda 表达式的封闭局部变量或者迭代器块中的局部变量时,Guid 会存储在堆上。
Guid 也可能既不存储在 GC 堆中,也不存储在堆栈中。 Guid 可能存储在完全非托管内存中,通过不安全的指针算术访问。
我很好奇为什么你如此关心 guid 的位是在堆栈上还是在堆上。这有什么区别呢?
Stop thinking that. Value types can have constructors too. It is perfectly legal, though strange, to say
That's the same as assigning zero to x.
Nope.
Yes. Guids are also stored on the stack.
Note that the analysis below assumes that the implementation of the CLI is the Microsoft "desktop" or "Silverlight" CLR running on Windows. I have no idea what other versions of the CLI do, what they do on Macs, and so on. If you need to know whether a particular hunk of memory is stored on the stack in other implementations, you'll have to ask someone who is an expert on those implementations.
A Guid is stored on the stack under the following circumstances:
(1) when the Guid is a "temporary" result of an ongoing calculation or is being used as an argument to a method. For example, if you have a method call M(new Guid()) then temporary storage for the new Guid is allocated on the stack.
(2) when the Guid is a local variable which is (a) not in an iterator block, (b) not a closed-over outer variable of an anonymous method or lambda expression.
In all other situations the Guid is not stored on the stack. A Guid is stored on the heap when it is a field of a reference type, an element of an array, a closed-over local of an anonymous method or lambda expression, or a local in an iterator block.
A Guid may also be stored in neither the GC heap nor the stack. A Guid might be stored in entirely unmanaged memory, accessed via unsafe pointer arithmetic.
I am curious as to why you care so much as to whether the bits of a guid are on the stack or on the heap. What difference does it make?
它是一种值类型。
http://msdn.microsoft.com/en-us/library/system .guid.aspx
It's a value type.
http://msdn.microsoft.com/en-us/library/system.guid.aspx
它实际上是 Guid。所有类型都是使用
new
关键字构造的。您可以根据值类型是否为类来识别引用类型和值类型, 界面,或委托(所有引用类型),或 结构 或 枚举(值类型)。It's actually Guid. All types are constructed using the
new
keyword. You can identify reference types from value types by whether they are a class, interface, or delegate (all reference types), or a struct or enum (value types).它的值类型,见下面的例子:
Its value type, See the below example: