Sunday, June 26, 2011

GUIDs (Globally Unique Identifiers) in .NET

.net

What is GUID ?

A GUID is a 128-bit integer that can be used to uniquely identify something. You may store as primary key in your database. A common approach is to create a auto incrementing integer, another way would be to create a GUID for your field. 
GUIDs can be generated in a number of ways; most often a hash of several things that might be unique at any given point in time like the IP address plus the clock date/time etc are used to generate Unique Ids. Each system may have slightly different algorithm to generate the Unique Id.



How to create GUID in C#?

In C#, System.GUID class represents a GUID in .NET Framework. System.GUID has static method NewGuid() , which generates a new Guid.

Example 

// This code example demonstrates the Guid.NewGuid() method.

using System;

class Sample 
{
    public static void Main() 
    {
    Guid g;
// Create and display the value of two GUIDs.
    g = Guid.NewGuid();
    Console.WriteLine(g);
    Console.WriteLine(Guid.NewGuid());
    }
}

/*
This code example produces the following results:

0f8fad5b-d9cb-469f-a165-70867728950e
7c9e6679-7425-40de-944b-e07fc1f90ae7

*/

GUID.Empty

As the Guid structure cannot be set to null, a value must be used to represent an ID that is not set. For that, GUID.Empty is a read-only instance of the GUID class whose value is guaranteed to be all zeros( 00000000-0000-0000-0000-000000000000 ). This is the same value that is generated by the parameter-less constructor. In order that a new Guid structure is not created every time a value must be compared with the empty GUID, the structure provides the Empty property. This property is read-only and always returns an empty GUID.

Example 

// This code example demonstrates the Guid.NewGuid() method.

using System;

class Sample 
{
    public static void Main() 
    {
    Guid guidObj = new GUID();
     
// Compare with guidObj with GUID.Empty
    Console.WriteLine(guidObj == GUID.Empty); // Returns true 
// Assign New GUID to guidObj
    guidObj = Guid.NewGuid();
 
// Again, compare with guidObj with GUID.Empty
    Console.WriteLine(guidObj); // Returns false
    
    }
}

GUID string formatting

As with all objects and structures, the GUID can be converted to a string using the ToString method. If the method is used without parameters, the GUID is formatted as a simple series of hexadecimal digits in the five groups described earlier.
The format of the converted string can be modified using a format specifier. This is passed as a string parameter to the ToString method.
The available format specifiers are listed in the table below.The format parameter can be "N", "D", "B", "P", or "X". If format is null or an empty string (""), "D" is used. The following table shows the accepted format specifiers for the format parameter. "0" represents a digit; hyphens ("-"), braces ("{", "}"), and parentheses ("(", ")") appear as shown.

Specifier Description Format of return value
N 32 digits 00000000000000000000000000000000
D 32 digits separated by hyphens 00000000-0000-0000-0000-000000000000
B 32 digits separated by hyphens, enclosed in braces {00000000-0000-0000-0000-000000000000}
P 32 digits separated by hyphens, enclosed in parentheses (00000000-0000-0000-0000-000000000000)
X Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces: {0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

Example

Console.WriteLine(Guid.Empty.ToString("N")); //00000000000000000000000000000000
Console.WriteLine(Guid.Empty.ToString("D")); //00000000-0000-0000-0000-000000000000
Console.WriteLine(Guid.Empty.ToString("B")); //{00000000-0000-0000-0000-000000000000}
Console.WriteLine(Guid.Empty.ToString("P")); //(00000000-0000-0000-0000-000000000000)
Console.WriteLine(Guid.Empty.ToString("X")); //{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

How to use in Microsoft SQL Server

We can generate GUID in Sql Server with the help of NEWID() function.
For more detail : NEWID() - Generate Randomly Sort Records - TSQL

References

No comments:

Post a Comment