New GUID all zeroes

There are two ways to make a new GUID in C#

I wanted to send a new GUID into SQL Server as a primary key for a record I was making, and it looked like the GUID wasn't initializing because the data looked like this:

00000000-0000-0000-0000-000000000000

Whilst that's technically a GUID (I guess), it's not much use to us as a unique identifier...!

This is the method I was using to make the GUID:

Guid myGuid = new Guid()

That didn't cause any compile errors, and the intellisense in Visual Studio seemed to lead me down that path, but it turns out that method (which I had actually been using elsewhere when getting GUIDs OUT of SQL Server) is for constructing C#=friendly GUIDs based on string / byte data you already have.

To make a NEW GUID, you need to use a slightly different method:

Guid myGuid = System.Guid.NewGuid()

That'll give you a freshly minted GUID that you can actually do something with.

www.cylindersix.com

This page is © Cylinder Six Limited and contains information about the issueNew GUID all zeroes