405

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

Which one is preferred?

josh3736
  • 139,160
  • 33
  • 216
  • 263
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • 6
    @ClintonWard I'm practically new to C# and @ Bob2Chiv I can't run the project right now and was curious about it. – OscarRyz Aug 13 '12 at 16:21
  • 7
    @OscarRyz - For quickly testing code in C#, I use [LinqPad](http://linqpad.net). – DaveShaw Aug 13 '12 at 20:35
  • 5
    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() – Michiel Cornille Mar 14 '13 at 10:05
  • 3
    @DaveShaw - 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. – Mark Meuer Aug 24 '16 at 16:06
  • There's also http://csharppad.com/, for when you don't have VS.Net handy... – T.J. Crowder Feb 19 '17 at 15:38
  • 1
    @MichielCornille Just got to know the feeling bro... – Jins Peter Jan 09 '19 at 14:45

5 Answers5

675

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.

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
MarkPflug
  • 28,292
  • 8
  • 46
  • 54
  • 33
    An empty UUID is very useful indeed. It makes a great special value. – Jon Hanna Aug 13 '12 at 16:21
  • 124
    On a side note, `new Guid()` is equivalent to `Guid.Empty`. – Steve Guidi Aug 13 '12 at 16:24
  • 36
    @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. – MarkPflug Aug 13 '12 at 16:27
  • 7
    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. – Jon Hanna Aug 13 '12 at 16:44
  • 1
    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). – MarkPflug Aug 13 '12 at 17:13
  • 7
    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. – PeterL Aug 13 '12 at 19:32
  • 2
    @Steve Guidi - and default(Guid) – Robert Fraser Dec 20 '13 at 02:15
  • @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() ;) – Mafii Jul 14 '16 at 11:55
  • 2
    @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). – Jeppe Stig Nielsen Aug 16 '16 at 10:31
  • 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.Empty – DanteTheSmith Jul 13 '17 at 12:53
46

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.

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • 18
    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 GUIDs – Clinton Ward Aug 13 '12 at 16:20
  • 10
    @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. – Jon Hanna Aug 13 '12 at 16:23
  • 3
    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 though – Clinton Ward Aug 13 '12 at 16:34
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()

Sudhanshu Mishra
  • 6,523
  • 2
  • 59
  • 76
  • [here](https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/shared/System/Guid.Windows.cs) is the implement of Guid.NewGuid() on Windows – Tyr Jul 06 '22 at 03:08
-1

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.

Yusuf Alp
  • 41
  • 8
-2

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.

Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Sharath
  • 31
  • 12
    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? – Dinerdo Sep 04 '18 at 15:06