ShortGuid - A shorter and url friendly GUID class in C#

posted on 20th Dec 2007

Update 2019-11-22: Code for this post has been moved to GitHub with a NuGet Package now available.

I like Mads Kristensen, he's often coming up with useful code snippets which he shares with the community AND he heads up the BlogEngine.NET project. He's produced the GuidEncoder helper class, which takes a standard guid like this:

c9a646d3-9c61-4cb7-bfcd-ee2522c8f633

And shortens it to a smaller string like this:

00amyWGct0y_ze4lIsj2Mw

Which is a huge help if you happen to be using guids in your URL's. You can read his article, A shorter and URL friendly GUID, on his website.

I've gone one step further than Mads, and created a struct which encapsulates the GuidEncoder functionality. I've called mine the "ShortGuid", but you could equally call it a "Sguid" if you like how that sounds (and if you pronounce GUID like "squid" and not "goo-id"). Mads's code for en/decoding the guid is still in there as static methods, which you can see further down - I'm pretty sure it hasn't been changed.

I'll jump right into the usage, if you're interested in the code, jump to the bottom and download the source visit the repo on GitHub.

Installation

Available on NuGet. To install, run the following command in the Package Manager Console:

PM> Install-Package CSharpVitamins.ShortGuid

Using the ShortGuid

The ShortGuid is compatible with normal Guid's and other ShortGuid strings. Let's see an example:

Guid guid = Guid.NewGuid();
ShortGuid sguid1 = guid; // implicitly cast the guid as a shortguid
Console.WriteLine(sguid1);
Console.WriteLine(sguid1.Guid);

This produces a new guid, uses that guid to create a ShortGuid, and displays the two equivalent values in the console. Results would be something along the lines of:

FEx1sZbSD0ugmgMAF_RGHw b1754c14-d296-4b0f-a09a-030017f4461f

Or you can implicitly cast a string to a ShortGuid as well.

string code = "Xy0MVKupFES9NpmZ9TiHcw";
ShortGuid sguid2 = code; // implicitly cast the string as a shortguid
Console.WriteLine(sguid2);
Console.WriteLine(sguid2.Guid);

Which produces the following:

Xy0MVKupFES9NpmZ9TiHcw
540c2d5f-a9ab-4414-bd36-9999f5388773

Flexible with your other data types

The ShortGuid is made to be easily used with the different types, so you can simplify your code. Take note of the following examples:

// for a new ShortGuid, just like Guid.NewGuid()
ShortGuid sguid = ShortGuid.NewGuid();

// to cast the string "myString" as a ShortGuid,
string myString = "Xy0MVKupFES9NpmZ9TiHcw";

// the following 3 lines are equivalent
ShortGuid sguid = new ShortGuid(myString); // traditional
ShortGuid sguid = (ShortGuid)myString;     // explicit cast
ShortGuid sguid = myString;                // implicit cast

// Likewise, to cast the Guid "myGuid" as a ShortGuid
Guid myGuid = new Guid("540c2d5f-a9ab-4414-bd36-9999f5388773");

// the following 3 lines are equivalents
ShortGuid sguid = new ShortGuid(myGuid); // traditional
ShortGuid sguid = (ShortGuid)myGuid;     // explicit cast
ShortGuid sguid = myGuid;                // implicit cast

After you've created your ShortGuid's the 3 members of most interest are the original Guid value, the new short string (the short encoded guid string), and the ToString() method, which also returns the short encoded guid string.

sguid.Guid;       // gets the Guid part
sguid.Value;      // gets the encoded Guid as a string
sguid.ToString(); // same as sguid.Value

Easy comparison with guid's and strings

You can also do equals comparison against the three types, Guid, string and ShortGuid like in the following example:

Guid myGuid = new Guid("540c2d5f-a9ab-4414-bd36-9999f5388773");
ShortGuid sguid = (ShortGuid)"Xy0MVKupFES9NpmZ9TiHcw";

if (sguid == myGuid)
	// logic if guid and sguid are equal

if (sguid == "Xy0MVKupFES9NpmZ9TiHcw")
	// logic if string and sguid are equal

ShortGuid Source Code

[Update 2019-11-22]
The original 2007 code snippet has been removed from this post in favour of hosting on GitHub with a NuGet Package now also available.

The only notable change has been to make the struct immutable i.e. removing the settings for the public properties (Thanks Matthew from the comments).