Getting Started
Installation
Install the UUID package via NuGet using one of the following methods:
# .NET CLI
dotnet add package UUID
# Package Manager Console
Install-Package UUID
Basic Usage
Here's how to get started with basic UUID operations:
// Generate a new UUID
var id = new UUID();
// Convert to string
string str = id.ToString(); // "0123456789abcdef0123456789abcdef"
// Parse from string
UUID parsed = UUID.Parse("0123456789abcdef0123456789abcdef");
String Formats
UUID supports multiple string formats for different use cases:
var uuid = new UUID();
// Standard format
string standard = uuid.ToString();
// "0123456789abcdef0123456789abcdef"
// Base32 format (URL-safe)
string base32 = uuid.ToBase32();
// "028T5CY4TQKFF028T5CY4TQKFF"
// Base64 format
string base64 = uuid.ToBase64();
// "782riWdFIwHvzauJZ0UjAQ=="
// Convert Base64 back to UUID
UUID fromBase64 = UUID.FromBase64(base64);
Console.WriteLine($"Base64 -> UUID: {fromBase64}");
// Safe parsing with TryFromBase64
if (UUID.TryFromBase64(base64, out UUID parsedFromBase64))
{
Console.WriteLine($"Successfully parsed from Base64: {parsedFromBase64}");
}
Byte Array Operations
UUID provides methods for converting to and from byte arrays:
// Convert UUID to byte array
UUID id = UUID.New();
byte[] bytes = id.ToByteArray();
Console.WriteLine($"UUID -> Bytes: {BitConverter.ToString(bytes)}");
// Convert byte array back to UUID
UUID fromBytes = UUID.FromByteArray(bytes);
Console.WriteLine($"Bytes -> UUID: {fromBytes}");
// Safe parsing with TryFromByteArray
if (UUID.TryFromByteArray(bytes, out UUID parsedFromBytes))
{
Console.WriteLine($"Successfully parsed from bytes: {parsedFromBytes}");
}
// Writing directly to a byte array
byte[] destination = new byte[16];
if (id.TryWriteBytes(destination))
{
Console.WriteLine($"Successfully wrote to byte array: {BitConverter.ToString(destination)}");
}
Performance Tips
String Formatting
- Use appropriate string format methods based on your needs
- Consider using Base32 for URL-safe strings
Bulk Operations
- Use array pooling for bulk operations
- Consider parallel processing for large batches
Memory Usage
- UUID struct is optimized for minimal memory footprint
- Use array pooling for bulk operations