What Is GUID In C#

GUID (or UUID) is an acronym for 'Globally Unique Identifier' (or 'Universally Unique Identifier'). A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.

What is GUID? 

GUID (or UUID) is an acronym for 'Globally Unique Identifier' (or 'Universally Unique Identifier'). A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.

Why do we use GUID?

We use GUID to create a unique Id. A GUID makes a great primary key in the back-end database. While each generated GUID is not guaranteed to be unique, the total number of unique keys (3.40282366×1038) is so large that the probability of the same number being generated twice is very small. For an application using 10 billion random GUIDs, the probability of a coincidence is approximately 1 in a quintillion.(1030)

We may store users or products in our database and want somehow to uniquely identify each row in the database. A common approach is to create an auto-incrementing integer; another way would be to create a GUID for your products.

Where are GUIDs used?

We must use GUID when we have multiple independent system or user who are generating ID which must be unique. GUIDs are used in enterprise software development like C#, Java, and C++ as database keys or just about anywhere else, a truly unique identifier is required. GUID is also used to identify all interfaces and objects in COM programming. GUID produce by windows application can be used to identify particular file,application,component,databse entry.

Advantage of GUID

  • They really help in preventing data collisions.
  • Easy to merge database between different machine,network etc. as it prevent data collisions.
  • It allows you to know your primary key before a record is inserted.
  • It cannot be easily guessed.

Disadvantage of GUID

  • Each GUID occupies 16 bytes. Consider a situation where a database is storing 10000 entries; there, only GUID will occupy 160000 bytes.
  • Most of the primary keys are foreign keys in other tables. Only 5 entries of FK consume about 60 bytes to store only the GUID. Over time, it becomes the primary cause of performance issues.
  • Random GUID is generated using an algorithm that is out of order. For sorting, we have to use different algorithms as insertion timestamp.
  • GUIDs are not very useful for small databases.

What is the format of GUID? 

  • GUID is a structure available in System Namespace
  • GUIDs are most commonly written in text as a sequence of hexadecimal digits as such,

    • 3F2504E0-4F89-11D3-9A0C-0305E82C3301
    • Often braces are added to enclose the above format, as such:
    • {3F2504E0-4F89-11D3-9A0C-0305E82C3301}      
How to create GUID in C#?
  1. Directly From Visual Studio

    Open Visual Studio->Tools->Create GUID->Registry Format->New GUID.

    It will create a new GUID every time you click New GUID.
  1. In Console Application

    The GUID method System.Guid.NewGuid() initializes a new instance of the GUID class.
    1. using System;  
    2. namespace GUIDTest {  
    3.     class MainClass {  
    4.         static void Main(string[] args) {  
    5.             System.Guid guid = System.Guid.NewGuid();  
    6.             Console.WriteLine(guid.ToString());  
    7.             Console.ReadLine();  
    8.         }  
    9.     }  
Cheers to coding!!!