Skip to main content

C#.Net - Generate new guid in C#.Net | GUID Generator in C#.Net

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

For those of you who don't know, a GUID (pronounced goo'id - Globally unique identifier) is a 128-bit integer that can be used to uniquely identify something. You may store users or products in your database and you want somehow uniquely identify each row in the database. A common approach is to create a autoincrementing integer, another way would be to create a GUID for your products.

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.

GUIDs can be generated in a number of ways; most often a hash of several things that might be unique at any given point in time like the IP address plus the clock date/time etc are used to generate Unique Ids. Each system may have slightly different algorithm to generate the Unique Id.

The GUID method can be found in the System namespace. The GUID method System.Guid.NewGuid() initializes a new instance of the GUID class.

Sample code :

System.Guid guid = System.Guid.NewGuid();
String id = guid.ToString();

System.Guid.NewGuid () returns a Guid object. You can use the
.ToString() method to convert it to a string. In the above example, you may get
a string which look like the following:

Output:

"b91c9121-0a17-4b26-a09d-d5980eb532db"

There are also a few overloads available for those of you who want the GUID formatted in a particular fashion.

Specifier
Format of return value
N
32 digits:
00000000000000000000000000000000
D
32 digits separated by hyphens:
00000000-0000-0000-0000-000000000000
B
32 digits separated by hyphens, enclosed in braces:
{00000000-0000-0000-0000-000000000000}
P
32 digits separated by hyphens, enclosed in parentheses:
(00000000-0000-0000-0000-000000000000)
X
Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight
hexadecimal values that is also enclosed in braces:
{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

Sample code :


Response.Write(@"<br>System.Guid.NewGuid().ToString() = " + System.Guid.NewGuid().ToString());
Response.Write(@"<br>System.Guid.NewGuid().ToString(""N"") = " + System.Guid.NewGuid().ToString("N"));
Response.Write(@"<br>System.Guid.NewGuid().ToString(""D"") = " + System.Guid.NewGuid().ToString("D"));
Response.Write(@"<br>System.Guid.NewGuid().ToString(""B"") = " + System.Guid.NewGuid().ToString("B"));
Response.Write(@"<br>System.Guid.NewGuid().ToString(""P"") = " + System.Guid.NewGuid().ToString("P"));

OUTPUT:

System.Guid.NewGuid().ToString() = 43808755-271a-4fa7-9f3d-b15bd39a0916
System.Guid.NewGuid().ToString("N") = a6fb779a19c74b00ba1a14ceca1c7292
System.Guid.NewGuid().ToString("D") = fb4c7003-4321-4a12-9c35-34945e916767
System.Guid.NewGuid().ToString("B") = {3e3c1426-f26a-4a21-9143-f8d67cc8e093}
System.Guid.NewGuid().ToString("P") = (c9a192e8-84d4-46db-8723-0dee14c2e0aa)

Remarks

This is a convenient static method that you can call to get a new Guid.

The chance that the value of the new Guid will be all zeros or equal to any other Guid is very low. You can determine whether a GUID consists of all zeros by comparing it to Guid.Empty.

Also note that Guid only contains alphanumeric characters and none of non-alphanumeric character will be seen in GUID except "-";

Frequently asked questions about GUIDs.

A. 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.

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

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

C. How can we generate GUID with SQL Server?

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

D. Which namespace must be referenced to utilize the GUID attribute. ?

Answer:  System.Runtime.InteropServices namespace must be referenced to utilize the GUID attribute.

Comments

Popular posts from this blog

Sort Dictionary Based On Value In Asp.Net And C#.Net | Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

In this tutorial i am going to explain about how to sort dictionary object based on value in asp.net and C#.Net or convert unsorted dictionary to sorted dictionary object in C#.Net and VB.Net or Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     { ...

C# code to send mail using smtp from gmail,yahoo mail and live mail

Introduction: In my previous article I have explained about   How to bind/Unbind events in jQuery . In this article I am going to explain about how to send mail from ASP.Net using gmail,yahoomail and live mail credentials. Explanation: First Include the below namespaces in your code behind file. using System; using System.Net; using System.Net.Mail;