Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Guid considered a value type or reference type?

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?

like image 403
SoftwareGeek Avatar asked Feb 26 '10 19:02

SoftwareGeek


People also ask

What is the datatype of GUID in C#?

The GUID data type is a 16 byte binary data type. This data type is used for the global identification of objects, programs, records, and so on. The important property of a GUID is that each value is globally unique. The value is generated by an algorithm, developed by Microsoft, which assures this uniqueness.

Which data type is a reference type?

Examples of reference data types are class, Arrays, String, Interface, etc. Examples of primitive data types are int, float, double, Boolean, long, etc.

Is Dictionary value type or reference type?

In Swift, Array, String, and Dictionary are all value types. They behave much like a simple int value in C, acting as a unique instance of that data.

What are value type and reference types in C#?

Value types and reference types are the two main categories of C# types. A variable of a value type contains an instance of the type. This differs from a variable of a reference type, which contains a reference to an instance of the type.


4 Answers

Guid is a Value Type.

See MSDN. Note that Guid is a struct. All structs are Value Types.

like image 162
Randolpho Avatar answered Oct 02 '22 23:10

Randolpho


You can see the definition of a Guid yourself:

public struct Guid ... 

Or you can test it like this:

bool guidIsValueType = typeof(Guid).IsValueType; 

Quote: "GUID's are created using the new keyword which makes me think it's a reference type."

Structs can have constructors too, for example new DateTime(2012, 12, 23).

like image 24
Juliet Avatar answered Oct 03 '22 00:10

Juliet


GUID's are created using the new keyword which makes me think it's a reference type.

Stop thinking that. Value types can have constructors too. It is perfectly legal, though strange, to say

int x = new int();

That's the same as assigning zero to x.

Is this correct?

Nope.

Are GUID's stored on heap?

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?

like image 34
Eric Lippert Avatar answered Oct 02 '22 23:10

Eric Lippert


It's a value type.

http://msdn.microsoft.com/en-us/library/system.guid.aspx

like image 32
jinsungy Avatar answered Oct 02 '22 23:10

jinsungy