Guid.NewGuid() vs. new Guid()
Asked Answered
S

5

406

What's the difference between Guid.NewGuid() and new Guid()?

Which one is preferred?

Sorority answered 13/8, 2012 at 16:10 Comment(6)
@ClintonWard I'm practically new to C# and @ Bob2Chiv I can't run the project right now and was curious about it.Sorority
@Sorority - For quickly testing code in C#, I use LinqPad.Blastosphere
Actually I do believe this question is relevant, because it's confusing and I don't really "0000000-0000.." as the best default. I just ran into a problem where a client couldn't login to a system because somewhere deep inside the project new Guid() was called instead of NewGuid()Sherikasherill
@Blastosphere - LinQPad is great and I use it a lot. But I wanted to point out that Visual Studio now has a "C# Interactive" window which very useful for these kinds of tests.Danny
There's also csharppad.com, for when you don't have VS.Net handy...Outpost
@MichielCornille Just got to know the feeling bro...Noguchi
A
682

new Guid() makes an "empty" all-0 guid (00000000-0000-0000-0000-000000000000 is not very useful).

Guid.NewGuid() makes an actual guid with a unique value, what you probably want.

Agosto answered 13/8, 2012 at 16:11 Comment(10)
An empty UUID is very useful indeed. It makes a great special value.Unorganized
On a side note, new Guid() is equivalent to Guid.Empty.Overstate
@JonHanna All guids make great special values. Unfortunately, the empty ones have a tendancy to collide. I agree that empty guids are useful though, typically to indicate that something is uninitialized.Agosto
The empty ones are very useful because they do collide. A special value that didn't collide with the known special value would be useless.Unorganized
I guess I don't understand your point. All guids collide with themselves, empty or not, and I would think that special values are special because they are known (ie compile time constants like COM identifiers).Agosto
I think you're both agreeing that it makes a good "known guid" to compare to, as in the case of indicating something is awaiting initialization or is in some other known state. Personally I'd prefer to use a null value, but I can see that somebody might need a "special" guid at some point, and the all-0 guid is probably the guid least likely to violate the principal of least surprise for future maintainers of the code.Educate
@Steve Guidi - and default(Guid)Ultracentrifuge
@SteveGuidi the implementation of Guid Empty is: public static readonly Guid Empty = new Guid(); - so its the other qay around, Guid.Empty is equal to new Guid() ;)Consumable
@SteveGuidi In a sense default(Guid) (or its C# 1.0 eqivalent new Guid()) is better than Guid.Empty because it is a compile-time constant. For example the method static void M(Guid optionalUuid = Guid.Empty) { /* ... */ } will not compile. If you use default(Guid) (or new Guid() which is the same) it works (optional parameter).Slavonic
new Guid() is equivalent to Guid.Empty? not really. Let's say you wanna have an optional parameter in a function. You can use parameter = new Guid() but not Guid.EmptyBeau
U
48

Guid.NewGuid() creates a new UUID using an algorithm that is designed to make collisions very, very unlikely.

new Guid() creates a UUID that is all-zeros.

Generally you would prefer the former, because that's the point of a UUID (unless you're receiving it from somewhere else of course).

There are cases where you do indeed want an all-zero UUID, but in this case Guid.Empty or default(Guid) is clearer about your intent, and there's less chance of someone reading it expecting a unique value had been created.

In all, new Guid() isn't that useful due to this lack of clarity, but it's not possible to have a value-type that doesn't have a parameterless constructor that returns an all-zeros-and-nulls value.

Edit: Actually, it is possible to have a parameterless constructor on a value type that doesn't set everything to zero and null, but you can't do it in C#, and the rules about when it will be called and when there will just be an all-zero struct created are confusing, so it's not a good idea anyway.

Unorganized answered 13/8, 2012 at 16:19 Comment(3)
I'll add this for fun. For a 1% chance of collision, you'd need to generate about 2,600,000,000,000,000,000 GUIDsKilloran
@ClintonWard it depends on which of the UUID algorithms are in use, but since some use MAC addresses to avoid collisions between machines, and all use a time factor to avoid collisions on the same machine, you need to create even more than the normal math would suggest. Unless you deliberately screw with the algorithm by changing the time and messing with MAC addresses, in which case you should get one pretty quickly - but that's deliberately messing with the inputs.Unorganized
That was v1 Guid. newer MS GUIDs are V4 and do not use the MAC address as part of the GUID generation. Time is still a factor thoughKilloran
B
17

[I understand this is an old thread, just adding some more detail] The two answers by Mark and Jon Hanna sum up the differences, albeit it may interest some that

Guid.NewGuid()

Eventually calls CoCreateGuid (a COM call to Ole32) (reference here) and the actual work is done by UuidCreate.

Guid.Empty is meant to be used to check if a Guid contains all zeroes. This could also be done via comparing the value of the Guid in question with new Guid()

So, if you need a unique identifier, the answer is Guid.NewGuid()

Biota answered 8/4, 2014 at 6:0 Comment(1)
here is the implement of Guid.NewGuid() on WindowsTablet
S
0

Guid.NewGuid() initializes a new instance of the Guid structure.

Example:

Guid g = Guid.NewGuid();
Console.WriteLine(g);

// This code example produces a result similar to the following:

// 0f8fad5b-d9cb-469f-a165-70867728950e

The method creates a Version 4 Universally Unique Identifier (UUID) as described in RFC 4122, Sec. 4.4

Also as mentioned in the official document,

The returned Guid is guaranteed to not equal Guid.Empty.

Substage answered 12/9, 2022 at 18:54 Comment(0)
R
-1

Guid.NewGuid(), as it creates GUIDs as intended.

Guid.NewGuid() creates an empty Guid object, initializes it by calling CoCreateGuid and returns the object.

new Guid() merely creates an empty GUID (all zeros, I think).

I guess they had to make the constructor public as Guid is a struct.

Rome answered 31/8, 2016 at 3:48 Comment(1)
What is the point of adding an answer that doesn't add anything which doesn't already exist in answers which have been here for years?Amnesty

© 2022 - 2024 — McMap. All rights reserved.