Home
Map
Guid ExampleUse the Guid class and the Guid.NewGuid method. With this method we can generate random strings.
C#
This page was last reviewed on Oct 11, 2023.
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.
byte, sbyte
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 tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
No updates found for this page.
Home
Changes
© 2007-2024 Sam Allen.