DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

C# & Databases - Creating A Unique Identifer

05.10.2006
| 49364 views |
  • submit to reddit
        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(); 
    

Comments

Snippets Manager replied on Fri, 2009/11/27 - 6:49am

System.Guid desiredGuid = System.Guid.NewGuid(); will produce a new unique/random guid. To create a specific guid you need to go: System.Guid desiredGuid = new System.Guid("96d36892-6eed-400c-91f8-a194f3522fae");

Snippets Manager replied on Fri, 2009/11/27 - 6:49am

No need to define blankGuid. The framework already has: Guid.Empty