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.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.
Sample code :
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.
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
Post a Comment