-
-
Notifications
You must be signed in to change notification settings - Fork 2
Detailed Usage
Taiizor edited this page Dec 28, 2024
·
4 revisions
A UUID consists of two main components:
- 64-bit timestamp
- 64-bit random data
This structure enables:
- Time-based sorting
- Minimal collision probability
- Cryptographic security
UUID id = new UUID();
DateTimeOffset timestamp = id.Time;
// Timestamp is always UTC-based
Console.WriteLine($"UTC Timestamp: {timestamp:u}");
- 26 characters in length
- URL-safe characters
- Removed confusing characters (I, L, O, etc.)
string base32 = id.ToBase32();
- Standard Base64 encoding
- More compact representation
string base64 = id.ToBase64();
byte[] buffer = new byte[UUID.SIZE];
if (id.TryWriteBytes(buffer))
{
// Use buffer
}
char[] buffer = new char[32];
if (id.TryWriteStringify(buffer))
{
// Use string buffer
}
UUIDs are fully compatible with Guid:
// UUID -> Guid
UUID id = new UUID();
Guid guid = id.ToGuid();
// or
guid = id;
// Guid -> UUID
Guid originalGuid = Guid.NewGuid();
UUID fromGuid = UUID.FromGuid(originalGuid);
// or
fromGuid = originalGuid;
UUIDs support various comparison operators:
UUID id1 = new UUID();
UUID id2 = new UUID();
// Equality
bool equals = id1 == id2;
bool notEquals = id1 != id2;
// Ordering
bool lessThan = id1 < id2;
bool greaterThan = id1 > id2;
bool lessOrEqual = id1 <= id2;
bool greaterOrEqual = id1 >= id2;
// IComparable
int comparison = id1.CompareTo(id2);
The UUID library is designed to be thread-safe:
- Each thread has its own RandomNumberGenerator instance
- Immutable struct design
- Lock-free operations
// Example multi-threaded usage
var tasks = Enumerable.Range(0, 1000)
.Select(_ => Task.Run(() => new UUID()))
.ToArray();
await Task.WhenAll(tasks);
- Check our FAQ section
- Visit Debugging and Diagnostics
- Review Performance guidelines
- Join our Discord community
- Ask on Stack Overflow
- Report issues on GitHub