Saturday, June 7, 2014

How to create a new GUID in C#

In this small tutorial I will show you how to create a GUID in your ASP.NET application.

But what is actually GUID is?

According to Wikipedia
A Globally Unique Identifier (GUID) is a unique reference number used as an identifier in computer software. The term GUID typically refers to various implementations of the universally unique identifier (UUID) standard.
There are two ways to create a new GUID.
  1. Guid.NewGuid() [NewGuid() method]
  2. new Guid() 
In the first method it always return a valid and unique GUID every time. But the second one is produced a bit of zero(0), like 00000000-0000-0000-0000-000000000000. But in the first one you will get an unique id every time like 8bd3ee4f-8069-4175-a6d1-b990e9e1a633.

So you can use the following code easily to generate a new GUID in your ASP.NET project.

protected void btnGenerateGuid_Click(object sender, EventArgs e)
{
   /* Createing a new Guid */
   String guid = Guid.NewGuid().ToString();
   /* Print the output */
   Response.Write(guid.ToString());
}

0 comments:

Post a Comment

Popular Posts

Pageviews