As another code exercise, I decided to look at RFC 4122 (“A Universally Unique IDentifier (UUID) URN Namespace”) and create an object in C# that was capable of handling all five versions presented in that document. I wanted to see if I could successfully interpret the algorithms as described by the document without looking at any reference implementations.

I think in the end it was quite successful and offers a pretty robust implementation of version 1 through 5 UUIDs, including a version 4 UUID which is compatible with the native System.Guid implementation. There is one class called Uuid which can represent any version of UUID, and it is used similar to the aforementioned System.Guid to either create new unique identifiers or represent existing ones. It also supports explicit casts to/from System.Guid.

Uuid existingUuid = new Uuid("9b1ea5f8-702b-4ad5-97f0-34bb10a28602");
Uuid newUuid = Uuid.NewUuid(Uuid.UUID_VERSION_1);

A battery of unit tests is also included to help verify the implementation. You can download the code from my code samples page as usual.

posted on June 1st, 2010 at 9:12 am

← Back to articles