Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Guid in C#

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 80
    Comment on it

    Hi friends,
    To discuss this topic let's start with some obvious questions and try to answering them with suitable explanation(if needed).

     

    What is Guid in C#?

    Guid refers to Global Unique Identifier. It takes 16 bytes space in memory (128 bits). It consists of only alphanumeric characters except "-" which is used to separate the groups of alphanumeric characters combination.

    System namespace is required to create a Guid in C#. Guid keyword in C# refers to a structure and in coming example we'll be initializing an instance of Guid structure by calling its own static method NewGuid().

     

    Syntax for creating a Guid:

    Guid guid = Guid.NewGuid();

     

    Why do we need a Guid in C#?

    As it's full form suggests it's used to get a unique values in C# code. They can be used to uniquely identify records in an application. There could be situations when you are inserting data from web application and you want to define primary key value for database table, using the C# code and not from database. In such case you can initialize primary key parameter with Guid value and save with other parameters.

     

    Please have a look on following visual summary of what we just discussed.

     

    PersonalInfo (Database table)

    Columns
    ID                 varchar(40)
    FirstName     varchar(50)
    LastName     varchar(50)
    DOB             datetime

     

    Sample parameters passing from C# code while inserting information for a single person in above table "PersonalInfo".

     

    In the above table schema we are assuming "ID" field to be uniquely identifying records in PersonalInfo table. The following parameters most probably would be getting initialized using input fields in web form, except "ID" parameter which will be initializing with the following C# statement

    Guid.NewGuid().ToString()

     

    ID                 =     Guid.NewGuid().ToString()
    FirstName     =    "MyFirstName"
    LastName     =    "MyLastName"
    DOB             =    "5/17/2016"

     

    Though we can also create a guid in SQL Server using NEWID(), but as we are focussing on creating Guid in C# hence above example go with C# syntax.

     

    Common mistake while creating Guid

    While getting familiar with the Guid creating syntax, sometimes developers try to create an object of Guid and try to use it thinking that they will get the unique value, but using such syntax they get 16 byte Guid value with all "0"s.

     

    Following code snippet shows that common mistake

    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Incorrect way of creating Guid
                Guid guid = new Guid();
    
                Console.WriteLine("Guid = " + guid.ToString());
                Console.ReadKey();
            }
        }
    }

     

    Output:

    Guid = 00000000-0000-0000-0000-000000000000

     

    Code snippet showing right way of creating Guid is as follows:

    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Correct way of creating Guid
                Guid guid = Guid.NewGuid();
    
                Console.WriteLine("Guid = " + guid.ToString());
                Console.ReadKey();
            }
        }
    }

     

    Output:

    Guid = da3e6988-657b-40aa-975b-569420060484

     

    Is a Guid assured to be 100% unique everytime?

    Well its not guaranteed to be 100% unique everytime, but the possibility of ending up with identical Guids is very small. As it's a subject of in-depth technical discussion hence we will keep it out of scope for this introductory blog.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: