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.
Databases and interop. 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).
An example. This simple program uses Guid.NewGuid and creates a Guid. After creating the Guid, we use ToString to see the contents in string form.
Note The Guid does not change each time we access it. After initialization, we can reuse it many times.
Detail We can get bytes from the 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
Some research. UUIDs are a standardized format. They are often used in databases to enforce uniqueness. File systems (a kind of database) also uses these identifiers.
A summary. To get 16 unique bytes, the Guid.NewGuid method is ideal. We can print (or serialize) the data returned with ToString.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.