Brian's Blog

items I see across my tribes

GUIDs

February 06
by briancarter 6. February 2009 05:03

C# & Databases - Creating a unique identifier

GUIDs are required to uniquely identify items across entities.  They provide a way to match items sent between companies or disjoint systems.  Instead of going through the pain of trying to keep integer IDs synced up between environments (arch, dev, qa, ua, prod), use a GUID and its done.

Creating a new GUID in C# is not as straightforward as one would think. Calling the constructor of System.Guid() will produce a GUID of all zeroes. Using System.Guid.NewGuid() will produce the desired effect of a new, unique identifier.

// produces a "blank" GUID: 
'00000000-0000-0000-0000-000000000000'
System.Guid blankGuid = new System.Guid(); 
// produces a new, unique GUID: 
'96d36892-6eed-400c-91f8-a194f3522fae'
System.Guid desiredGuid = System.Guid.NewGuid(); 
// convert string to GUID
System.Guid MyGuid = new Guid(stringValue);

Categories: Development

Comments are closed

 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.