The Guid struct represents globally unique identifier in C#. The Guid is the 16 byte value and every time it generates a unique value. There are 2128 or 3.4 × 1038 possible unique values available by using Guid.
To generate new Guid value, open Microsoft Visual Studio and Create new console application and add below code.
using System;
namespace CSharpGuid
{
class Program
{
static void Main(string[] args)
{
Guid unique = Guid.NewGuid();
Console.WriteLine(unique.ToString());
Console.ReadLine();
}
}
}
It generates new unique value, displays as below.
To instantiate the existing value, use one of the constructors as shown below.
public Guid(byte[] b); // It takes a 16-byte array as input
public Guid(string g); // It takes a formatted string as input
The Guid is a struct that means value types. So, we can compare two Guid values with equality symbol. We can convert Guid into a byte array by using extension method ToByteArray(). Guid.Empty property returns an empty value.
tags:
guid struct, guid unique