GUIDs In C# And .NET

C# Guid. A GUID (Global Unique IDentifier) is a 128-bit integer used as a unique identifier. Learn how to create a GUID in C# and .NET using C# Guid class.

GUID stands for Global Unique Identifier. A GUID is a 128-bit integer (16 bytes) that you can use across all computers and networks wherever a unique identifier is required.
 
Here are some frequently asked questions about GUIDs.
 

How many GUIDs in Microsoft Windows can one computer generate without rolling over or running out?

 
Answer
 
Without getting into detail, let me tell you there are 2^122 or 5,316,911,983,139,663,491,615,228,241,121,400,000 possible combination.
 
Reason is beyond the scope of this article.
 

Which class in .NET Framework is used to generate Guid?

 
Answer
 
System.GUID class represents a GUID in .NET Framework.
 

How can we generate GUID with SQL Server?

 
Answer
 
We can generate GUID in Sql Server with the help of NEWID() function
 

Which namespace must be referenced to utilize the GUID attribute?

 
Answer
 
System.Runtime.InteropServices namespace must be referenced to utilize the GUID attribute.
 
This can be done as shown in following code,
  1. using System;    
  2. using System.Runtime.InteropServices;    
  3. namespace TESTGUID {    
  4.     [Guid("9245fe4a-d402-451c-b9ed-9c1a04247482")]    
  5.     class Example {    
  6.         //Your Implementation     
  7.     }    
  8. }   

Generating GUID in .NET Framework using C#

 
We can generate GUID by calling following code snippet. As you can see from this code, we use Guid.NewGuid() method to generate a new GUID in C#.
  1. using System;    
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;    
  5. namespace ConsoleApplication5 {    
  6.     class Program {    
  7.         static int Main(string[] args) {    
  8.             Guid obj = Guid.NewGuid();    
  9.             Console.WriteLine("New Guid is " + obj.ToString());    
  10.             Console.ReadLine();    
  11.             return -1;    
  12.         }    
  13.     }    
  14. }  
Note that in above code we have used the NewGuid Method which will create new GUID.
 
A common mistake done by C# developers is to create an object of GUID and trying to print that.
 
The following code snippet reflects that mistake.
  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. namespace ConsoleApplication5 {    
  6.     class Program {    
  7.         static int Main(string[] args) {    
  8.             Guid obj = new Guid();    
  9.             Console.WriteLine("New Guid is " + obj.ToString());    
  10.             Console.ReadLine();    
  11.             return -1;    
  12.         }    
  13.     }    
  14. }  
The output of above console program will always be 16 byte with all 0. Everytime same thing will come. So while generating GUID use NewGuid Method. Also note that Guid only contains alphanumeric characters and none of non-alphanumeric character will be seen in GUID except "-";
 
 
Hope it helps.