How to Create and Use a GUID in C# VB.Net Application

What is a GUID:
If you don’t know about GUID, a GUID (Globally unique identifier) is a 128-bit integer that can be used to uniquely identify a record or data. Specially we use GUID as a prefix or post-fix while we gave users to upload images or files in a same folder. Another common usage is to create a auto increment integer. So finally A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.

How to Create a GUID:
The GUID method can be found in the System namespace. The GUID method System.Guid.NewGuid() initializes a new instance of the GUID class. There are also a few overloads available for those of you who want the GUID formatted in a particular fashion. The following table shows the accepted format specifiers for the format parameter. Where “0” represents a digit; hyphens (“-“), braces (“{“, “}”), and parentheses (“(“, “)”) appear as shown:

Specifier Format of return value
N 32 digits:
D 32 digits separated by hyphens:
B 32 digits separated by hyphens, enclosed in braces:
P 32 digits separated by hyphens, enclosed in parentheses:
X Four hexadecimal values enclosed in braces, where the fourth value is a subset of eight hexadecimal values that is also enclosed in braces:

Generate GUID in C#:

        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"));
        Response.Write(@"<br>System.Guid.NewGuid().ToString(""X"") = " + System.Guid.NewGuid().ToString("X"));

Generate GUID in VB.Net:

        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"))
        Response.Write("<br>System.Guid.NewGuid().ToString(""X"") = " & System.Guid.NewGuid().ToString("X"))


Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *