After an interval of more than 2 weeks, I am back online blogging. Today I will share a quick tip on how to create a guid in C# and vb.net. For a description about GUIDs, visit this
Wikipedia page.A guid in the .net world is represented by the System.Guid class. The Guid class has many constructors which takes 0 or more arguments and constructs a guid from it. However, if we want to create a random guid, using the parameter less constructor would not do. A guid created using the parameter less constructor contains all zeros. So how to create a unique guid?
Guid.NewGuid() method
The Guid class has a static method named NewGuid() which creates and returns a new random guid. Let's see a example.
- C#
Guid g=Guid.NewGuid(); MessageBox.Show(g.ToString());
- VB.NET
Dim g As Guid =Guid.NewGuid() MessageBox.Show(g.ToString())
Here we are simply creating a new guid and using ToString() method for displaying it. You can also get byte representation of the guid using ToByteArray() method.Enjoy!