Examples
Basic Examples
Generate and Format UUIDs
// Generate a new UUID
var id = new UUID();
// Different string formats
Console.WriteLine(id.ToString()); // Standard format
Console.WriteLine(id.ToBase32()); // Base32 format
Console.WriteLine(id.ToBase64()); // Base64 format
// Get raw bytes
byte[] bytes = id.ToByteArray();
This example shows different ways to create and format UUIDs.
Parse UUIDs
// Parse from string
var id1 = UUID.Parse("0123456789abcdef0123456789abcdef");
// Try parse pattern
if (UUID.TryParse("0123456789abcdef0123456789abcdef", out var id2))
{
Console.WriteLine("Successfully parsed: " + id2);
}
Examples of parsing UUIDs from different string formats.
Advanced Examples
Bulk UUID Generation
// Generate multiple UUIDs efficiently
UUID[] uuids = new UUID[1000];
ArrayExtension.Fill(uuids);
// Process in parallel
Parallel.ForEach(uuids, uuid =>
{
ProcessUUID(uuid);
});
private void ProcessUUID(UUID uuid)
{
// Your processing logic here
var str = uuid.ToString();
// ...
}
Example of generating and processing multiple UUIDs efficiently.
Custom Formatting
var uuid = new UUID();
// Custom string builder formatting
var sb = new StringBuilder();
sb.Append("UUID-");
sb.Append(uuid.ToString());
sb.Append("-END");
// Format with prefix
string prefixed = $"ID_{uuid}";
Examples of custom UUID string formatting.
String Format Examples
String Format Conversions
// Create a new UUID
var id = new UUID();
// Convert to different formats
string standard = id.ToString();
string base32 = id.ToBase32();
string base64 = id.ToBase64();
Console.WriteLine($"Standard: {standard}");
Console.WriteLine($"Base32: {base32}");
Console.WriteLine($"Base64: {base64}");
// Convert back from Base64
UUID fromBase64 = UUID.FromBase64(base64);
Console.WriteLine($"Restored from Base64: {fromBase64}");
// Safe parsing from Base64
if (UUID.TryFromBase64(base64, out UUID parsed))
{
Console.WriteLine($"Successfully parsed from Base64: {parsed}");
}
Examples of converting UUIDs to and from different string formats.
Byte Array Operations
// Create a new UUID
var id = new UUID();
// Convert to byte array
byte[] bytes = id.ToByteArray();
Console.WriteLine($"As bytes: {BitConverter.ToString(bytes)}");
// Convert back from bytes
UUID fromBytes = UUID.FromByteArray(bytes);
Console.WriteLine($"Restored from bytes: {fromBytes}");
// Safe conversion with TryFromByteArray
if (UUID.TryFromByteArray(bytes, out UUID parsed))
{
Console.WriteLine($"Successfully parsed from bytes: {parsed}");
}
// Write directly to a byte array
byte[] destination = new byte[16];
if (id.TryWriteBytes(destination))
{
Console.WriteLine($"Successfully wrote to byte array: {BitConverter.ToString(destination)}");
}
Examples of working with byte array representations of UUIDs.
Guid Conversions
// Create a new UUID
var uuid = new UUID();
// Implicit conversion to Guid
Guid guid = uuid;
Console.WriteLine($"As Guid: {guid}");
// Implicit conversion from Guid
UUID fromGuid = guid;
Console.WriteLine($"Back to UUID: {fromGuid}");
// Using explicit methods
Guid guidExplicit = uuid.ToGuid();
UUID uuidExplicit = UUID.FromGuid(guidExplicit);
Examples of converting between UUID and Guid types.
Database Examples
Entity Framework Core
public class User
{
public UUID Id { get; set; }
public string Name { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.Property(e => e.Id)
.HasConversion(
v => v.ToString(),
v => UUID.Parse(v));
}
}
Example of using UUIDs with Entity Framework Core.
Dapper
public async Task<User> GetUser(UUID id)
{
using var connection = new SqlConnection(connectionString);
return await connection.QuerySingleOrDefaultAsync<User>(
"SELECT * FROM Users WHERE Id = @Id",
new { Id = id.ToString() }
);
}
Example of using UUIDs with Dapper.
Web API Examples
ASP.NET Core Controller
public class UserController : ControllerBase
{
[HttpGet("{id}")]
public async Task<ActionResult<User>> GetUser(UUID id)
{
var user = await _userService.GetUserAsync(id);
if (user == null)
return NotFound();
return user;
}
[HttpPost]
public async Task<ActionResult<User>> CreateUser(User user)
{
user.Id = new UUID(); // Generate new UUID
await _userService.CreateUserAsync(user);
return CreatedAtAction(nameof(GetUser), new { id = user.Id }, user);
}
}
Example of using UUIDs in ASP.NET Core Web API.