Coder Perfect

new Guid vs. Guid.NewGuid() ()

Problem

What makes new Guid() different from Guid.NewGuid()?

Which one do you prefer?

Asked by OscarRyz

Solution #1

create Guid() returns an all-0 guid that is “empty” (00000000-0000-0000-0000-000000000000 is not very useful).

Guid. NewGuid() generates a real guid with a unique value, which is presumably what you want.

Answered by MarkPflug

Solution #2

Guid. NewGuid() generates a new UUID with an approach that ensures that clashes are extremely improbable.

new Guid() generates an all-zero UUID.

In most cases, you’d prefer the former because that’s what a UUID is for (unless you’re getting it from somewhere else, of course).

In some circumstances, an all-zero UUID is desirable, but Guid is not one of them. Empty or default(Guid) expresses your goal more clearly, and there’s less likelihood that someone reading it will think a unique value was generated.

Because of this lack of clarity, new Guid() isn’t very useful, but it’s impossible to have a value-type without a parameterless constructor that produces an all-zeros-and-nulls value.

Actually, you can 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 regarding when it’ll be called and when an all-zero struct will be formed are complicated, so it’s not a good idea in any case.

Answered by Jon Hanna

Solution #3

[I realize this is an old thread, but I just wanted to provide some more information] The discrepancies are summarized in Mark and Jon Hanna’s answers, albeit it may be of interest to some that

Guid.NewGuid()

UuidCreate performs the actual work after calling CoCreateGuid (a COM call to Ole32) (reference here).

Guid. Empty is used to see if a Guid is entirely made up of zeros. This might also be accomplished by comparing the value of the Guid in question to the value of new Guid ()

So, if you’re looking for a unique identification, Guid is the way to go. NewGuid()

Answered by Sudhanshu Mishra

Solution #4

Guid. NewGuid() is useful since it generates GUIDs as expected.

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

new Guid() just returns an empty GUID (all zeros, I think).

Because Guid is a struct, I suppose they had to make the function Object() { [native code] } public.

Answered by Sharath

Post is based on https://stackoverflow.com/questions/11938151/guid-newguid-vs-new-guid