md.shortguid 1.0.0

dotnet add package md.shortguid --version 1.0.0
NuGet\Install-Package md.shortguid -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="md.shortguid" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add md.shortguid --version 1.0.0
#r "nuget: md.shortguid, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install md.shortguid as a Cake Addin
#addin nuget:?package=md.shortguid&version=1.0.0

// Install md.shortguid as a Cake Tool
#tool nuget:?package=md.shortguid&version=1.0.0

Short Guid

PM> NuGet\Install-Package md.shortguid

A V4 GUID represented in the form of a short(er), base64-encoded, url-safe, string.
With the option to replace version, and variant data for custom flags.

Guid Diagram

By default .net's Guid.NewGuid(), creates a Version "4", and Variant "DCE 1.1, ISO/IEC 11578:1996" UUID.
ShortGuid replaces these constant values with custom ones, allowing users to set flags in these locations.

A standard Guid has 36 characters: 2249d2d9-e29a-4c7c-9505-256760d13fad.
A ShortGuid has 22 characters: 2dJJIprifEyVBSVnYNE_rQ.

Guids, and ShortGuids can be converted to one another without any data loss.
ShortGuids are url safe.

Short Guid

var sg1 = new ShortGuid(); // Creates a new ShortGuid, generating a new Guid. 
var sg2 = new ShortGuid(Guid.NewGuid()); // Creates a ShortGuid, from an existing Guid.
var sg3 = ShortGuid.NewGuid(); // Same as the empty constructor, aligns with Guid's api.

Short Guid With Flags

// Int32 type flags, in the range of 0 - 63 (inclusive), allows for 64 unique values.
var flags = 63;
var sg1 = new ShortGuid(flags);

// As an alternative, enums can be used with up to 64 unique fields.
public enum Values
{
    A = 0,
    B = 1,
    ...
    Y = 62,
    Z = 63
}
var valueFlags = Values.Z;
var sg2 = new ShortGuid<Values>(valueFlags);

// You could use a flags enum with up to 7 combined values.
[Flags]
public enum Flags
{
    A = 0,
    B = 1,
    C = 2,
    D = 4,
    E = 8,
    F = 16,
    G = 32
}
var combinedFlags = Flags.A | Flags.B | Flags.C | Flags.D | Flags.E | Flags.F | Flags.G;
var sg3 = new ShortGuid<Flags>(combinedFlags);

Converting between Guids and Short Guids

// This example uses the provided extension methods, but it can be done though the ShortGuid type as well. 

Guid guidIn = Guid.NewGuid(); // e.g. "6a54c832-f9c5-4fc5-bdd2-c546fb513499".
int flagsIn = 42;

// Guid to ShortGuid:
string shortGuid = guidIn.ToShortGuid(flagsIn); // e.g. "MshUasX5xa_90sVG_1E0mQ".

// ShortGuid to Guid:
var (guidOut, flagsOut) = shortGuid.ToGuid();

// ShortGuid contains both the original guid, and flags data; while being 2/3 of the size (when comparing the string formats).
Assert.Equal(guidIn, guidOut);
Assert.Equal(flagsIn, flagsOut);

Credits

Changelog

1.0.0

  • ShortGuid type, with the options of using a plain V4 GUID, or one with custom flags.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 71 6/29/2024

Created a ShortGuid type, with the options of using a plain V4 GUID, or one with custom flags.