What Is GUID In C#

What is GUID?

Earlier GUID was known as UUID. UUID stands for Universally Unique Identifier. We use GUID for unique identifier. Now a question arise if we already have primary key than why we use Globally Unique Identifier(GUID). we use GUID because it have very low probability of being duplicated as it is 128-bit integer(16 bytes) which allow to use GUID across all databse and computer without data collision.

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?

Consider an distibuted system where many programmer are working independently and generating ID, in this senario it is possible that two programmer generate common ID and at time of merging it will create problem so we must use GUID when we have multiple independent system or user who are generating ID which must be unique. In programming whenever we create a interface or method it is given an unique id which will be used to invoke them. This unique ID is GUID. 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.
    using System;  
    namespace GUIDTest {  
        class MainClass {  
            static void Main(string[] args) {  
                System.Guid guid = System.Guid.NewGuid();  
                Console.WriteLine(guid.ToString());  
                Console.ReadLine();  
            }  
        }  
    }

Cheers to coding!!!


Similar Articles