What's the difference between Guid.NewGuid()
and new Guid()
?
Which one is preferred?
What's the difference between Guid.NewGuid()
and new Guid()
?
Which one is preferred?
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.
new Guid()
is equivalent to Guid.Empty
. –
Overstate public static readonly Guid Empty = new Guid();
- so its the other qay around, Guid.Empty is equal to new Guid() ;) –
Consumable 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 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.
[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()
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.
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
.
© 2022 - 2024 — McMap. All rights reserved.