Guid
A Guid
is a "globally unique identifier." This means the value will not conflict with another Guid
. We can use Guids to enforce uniqueness.
We find that databases can use Guids to provide a unique identifier for a row of data. And Guids are used in software systems (file systems and COM).
This simple program uses Guid.NewGuid
and creates a Guid
. After creating the Guid
, we use ToString
to see the contents in string
form.
Guid
does not change each time we access it. After initialization, we can reuse it many times.Guid
. We can loop over or test these 16 bytes.using System; class Program { static void Main() { // Generate new Guid. var result = Guid.NewGuid(); // Print the Guid as a string. // ... It does not change. Console.WriteLine(result.ToString()); Console.WriteLine(result.ToString()); // Count the bytes. var array = result.ToByteArray(); Console.WriteLine("BYTE LENGTH:" + array.Length); } }a2934fa2-6f7e-4ac9-8210-681814ac86c4 a2934fa2-6f7e-4ac9-8210-681814ac86c4 BYTE LENGTH:16
UUIDs
are a standardized format. They are often used in databases to enforce uniqueness. File systems (a kind of database) also uses these identifiers.
To get 16 unique bytes, the Guid.NewGuid
method is ideal. We can print (or serialize) the data returned with ToString
.