Is Guid considered a value type or reference type?
Asked Answered
C

6

40

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?

Coracorabel answered 26/2, 2010 at 19:21 Comment(0)
O
38

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).

Ozone answered 26/2, 2010 at 19:24 Comment(2)
No it's a Value Type -> see @Randolpho's answerHaily
@Haily Actually, you read the answer wrong way.Empathy
S
46

Guid is a Value Type.

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

Smashed answered 26/2, 2010 at 19:22 Comment(2)
Except of course for System.ValueType which is in fact a class :)Goodlooking
@JaredPar: True, but it's also abstract, so no danger of instantiation.Smashed
O
38

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).

Ozone answered 26/2, 2010 at 19:24 Comment(2)
No it's a Value Type -> see @Randolpho's answerHaily
@Haily Actually, you read the answer wrong way.Empathy
T
21

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?

Teague answered 26/2, 2010 at 20:43 Comment(2)
well, now that it's clear guid can be stored anywhere, i guess it wouldn't matter.Coracorabel
When writing soft-real-time applications (animation, games, some UI work), it's often necessary to reduce, amortize, or eliminate GC allocations within a specific "loop" or iteration of the software. Doing so reduces or eliminates GC collections within those loops, which cause animation "hitches" that are visible to the user. Thus, knowing whether a particular line of code "allocs" is needed to decide when to cache objects or use object pooling. Example: smooth realtime physics simulations written entirely in C# must not allocate in either the collide or integration phases.Isabelisabelita
M
7

It's a value type.

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

Miltonmilty answered 26/2, 2010 at 19:22 Comment(0)
S
7

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).

Secunda answered 26/2, 2010 at 19:26 Comment(1)
You might want to add enum to your list of value types.Knar
J
5

Its value type, See the below example:

using System;                   
public class Program
{
    public static void Main()
    {
        Guid a1 = new Guid();
        Console.WriteLine(a1);
        Guid b1 = a1;
        Console.WriteLine(b1);
        a1 = Guid.NewGuid();
        Console.WriteLine(a1);
        Console.WriteLine(b1);
    }
}

/* OUTPUT
00000000-0000-0000-0000-000000000000
00000000-0000-0000-0000-000000000000
164f599e-d42d-4d97-b390-387e8a80a328
00000000-0000-0000-0000-000000000000
*/
Jackal answered 3/4, 2018 at 4:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.