What is GUID ? How to generate a GUID ?

What is GUID ?


GUID stands for Globally Unique IDentifier. It is a string of numbers and/or letters.

A GUID represents a Unique identifier - means you cannot generate the same GUID more than once. A GUID has a very low (...practically impossible) probability of being duplicated 

GUIDs have a variety of applications. You can use a GUID as a primary key in your database table or in a several other scenarios. A good example would be, if you have a distributed application where data is generated and stored in various locations and you want to merge all those data at some intervals of time, you may use GUID as the primary key.


How to write a GUID in c#.


GUID newguid = GUID.NewGuid(); // this will create a 36 characters 
Outputs: 12345678-1234-1234-1234-123456789abc



How to apply limit the length of GUID?

string character= Guid.NewGuid().ToString().Substring(0, Guid.NewGuid().ToString().IndexOf("-"));


this will generate a unique alpha numeric charcter

E.g 

if the generated GUID is "247d4265-273c-4b62-b6eb-bf00241d77aa" then it will retrieve only "247d4265".
It will be useful for generate a unique character.



Format the GUID?

Guid.NewGuid().ToString() => 36 characters (Hyphenated)
outputs: 12345678-1234-1234-1234-123456789abc

Guid.NewGuid().ToString("D") => 36 characters (Hyphenated, same as ToString())
outputs: 12345678-1234-1234-1234-123456789abc

Guid.NewGuid().ToString("N") => 32 characters (Digits only)
outputs: 12345678123412341234123456789abc

Guid.NewGuid().ToString("B") => 38 characters (Braces)
outputs: {12345678-1234-1234-1234-123456789abc}

Guid.NewGuid().ToString("P") => 38 characters (Parentheses)
outputs: (12345678-1234-1234-1234-123456789abc)

Guid.NewGuid().ToString("X") => 68 characters (Hexadecimal)

outputs: {0x12345678,0x1234,0x1234,{0x12,0x34,0x12,0x34,0x56,0x78,0x9a,0xbc}}





Comments

Popular posts from this blog

Handle and show SQL Exception show in popup message box in SAP ABAP

How to use GUID in asp.net C# for unique ID?

How to access an internal table of one program in another program