Is Guid a sign of value type or reference type?

Guides are created using the new keyword, which makes me think that this is a reference type.

Is it correct?

Guid uid = new Guid();

Are guides stored on the heap?

+22
c # guid value-type reference-type
Feb 26 '10 at 7:21
source share
5 answers

You yourself can see the definition of Guid:

 public struct Guid ... 

Or you can check it as follows:

 bool guidIsValueType = typeof(Guid).IsValueType; 

GUIDs are created using a new keyword, which makes me think that this is a reference type.

Structures can also have constructors, for example new DateTime(2012, 12, 23) .

+27
Feb 26 2018-10-28
source share

Guid is a value type.

See MSDN . Note that Guid is a struct . All structures Value types .

+27
Feb 26 '10 at 7:22
source share

GUIDs are created using the new keyword, which makes me think that this is a reference type.

Stop thinking about it. Value types can also have constructors. It is perfectly legal, albeit strange, to say

 int x = new int(); 

Same as zero assignment x.

It is right?

Nope.

Are GUIDs stored on the heap?

Yes. Guides are also stored on the stack.

Note that the analysis below assumes that the CLI implementation is the Microsoft CLR "desktop" or "Silverlight" running on Windows. I have no idea what other versions of the CLI do, what they do on Macs, etc. If you need to know if a particular piece of memory is stored on the stack in other implementations, you need to ask someone who is an expert in these implementations.

The manual is stored on the stack under the following circumstances:

(1) when Guid is the “temporary” result of the current calculation or is 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 Guid is a local variable that is (a) not in an iterator block, (b) is not a closed external variable of an anonymous method or lambda expression.

In all other situations, Guid is not stored on the stack. The guide is stored on the heap when it is a reference type field, an array element, a private local anonymous method, or a lambda expression or local in an iterator block.

The manual can also be stored on either the GC heap or on the stack. The manual can be stored in completely unmanaged memory, accessed through unsafe pointer arithmetic.

I am curious why you care about whether the guid bits are on the stack or on the heap. What's the difference?

+13
Feb 26 '10 at 20:43
source share
+7
Feb 26 '10 at 7:22
source share

Actually Guid . All types are created using the new keyword. You can identify link types from value types, regardless of whether they are class , interface, or delegate (all reference types) or struct or enum (value types).

+5
Feb 26 2018-10-28
source share



All Articles