![]() |
VOOZH | about |
dotnet add package shortid --version 5.1.1
NuGet\Install-Package shortid -Version 5.1.1
<PackageReference Include="shortid" Version="5.1.1" />
<PackageVersion Include="shortid" Version="5.1.1" />Directory.Packages.props
<PackageReference Include="shortid" />Project file
paket add shortid --version 5.1.1
#r "nuget: shortid, 5.1.1"
#:package shortid@5.1.1
#addin nuget:?package=shortid&version=5.1.1Install as a Cake Addin
#tool nuget:?package=shortid&version=5.1.1Install as a Cake Tool
<div align="center"> <img height="150" width="150" src="https://raw.githubusercontent.com/bolorundurowb/shortid/refs/heads/master/assets/logo.svg" alt="shortID logo" /> </div>
π Build, Test & Coverage
π codecov
π NuGet Version
ShortId is a small C# library for generating random, URL-friendly identifiers. It targets .NET Standard 2.0, so you can use it from modern .NET and .NET Framework projects. Generation is thread-safe and tuned for low allocation.
Typical uses include primary keys, opaque tokens, and any place you want a compact random string instead of a GUID.
Package Manager
Install-Package shortid
.NET CLI
dotnet add package shortid
PackageReference (pin the version you want from NuGet)
<PackageReference Include="shortid" Version="5.1.1" />
Add the namespace:
using shortid;
Call ShortId.Generate() with no arguments to use the default options:
string id = ShortId.Generate();
// Example: KXTR_VzGVUoOY
Default behaviour (see Options for details):
length in ShortIdOptions._ and -) may appear; numbers are off by default.Configure generation with ShortIdOptions:
| Parameter | Default | Description |
|---|---|---|
useNumbers |
false |
Include 0-9 in the character pool. |
useSpecialCharacters |
true |
Include _ and - in the pool. |
length |
15 | Exact output length. Must be at least 8. |
generateSequential |
false |
Prefix IDs with a time-based component so lexicographic order follows generation order. |
Examples:
// Include digits
var withNumbers = new ShortIdOptions(useNumbers: true);
string id1 = ShortId.Generate(withNumbers);
// No special characters
var noSpecials = new ShortIdOptions(useSpecialCharacters: false);
string id2 = ShortId.Generate(noSpecials);
// Fixed length (minimum 8)
var fixedLength = new ShortIdOptions(length: 9);
string id3 = ShortId.Generate(fixedLength);
// Sequential / monotonic IDs (time prefix + random suffix)
var sequential = new ShortIdOptions(generateSequential: true);
string id4 = ShortId.Generate(sequential);
When generateSequential is true, the first six characters encode the current time (centiseconds since the library epoch) in Base85; the rest of the string is random from the active pool. Shorter total lengths leave less room for randomness after the prefix, so prefer 12+ characters for high burst rates.
Custom character set: replace the default pool (letters only before numbers/specials are added per options). The string must contain at least 50 unique non-whitespace characters; duplicates and whitespace are stripped.
string characters = "βΆβ·βΈβΉβΊβ»βΌβ½βΎβΏββββββ
βββββββββββββββββββββββββββ β‘β’β£β€β₯β¦β§β¨β©β β‘β’β£β€β₯β¦β§β¨β©βͺβ«"; // your alphabet, 50+ unique chars
ShortId.SetCharacters(characters);
Reproducible sequences: set a fixed seed for the internal Random instance (useful for tests; avoid for security-sensitive IDs in production):
ShortId.SetSeed(1939048828);
Reset: restore default character pool and a new unseeded random generator:
ShortId.Reset();
SetCharacters, SetSeed, and Reset are synchronised and safe to call from multiple threads together with Generate.
Generate(null) throws ArgumentNullException.length below 8 throws ArgumentException.SetCharacters input throws ArgumentException or InvalidOperationException as documented in code.Throughput and allocation depend on your CPU, OS, and .NET runtime. Measure on your own hardware using the BenchmarkDotNet project in this repository:
cd src
dotnet run -c Release --project shortid.Benchmarks
Optional: append -- -j short for a quicker run. Any BenchmarkDotNet CLI arguments go after --.
One complete run on Windows 11, Intel Core Ultra 7 265K, .NET 8.0.26, BenchmarkDotNet 0.14.0. Absolute figures will differ on other hardware; relative gaps between configurations tend to hold.
Length sweep (ShortIdLengthBenchmarks): letters + _/-, no numbers, varying length:
| Length | Mean | Allocated |
|---|---|---|
| 8 | 35.2 ns | 208 B |
| 9 | 39.6 ns | 216 B |
| 10 | 41.9 ns | 224 B |
| 11 | 39.0 ns | 224 B |
| 12 | 40.4 ns | 224 B |
| 13 | 42.0 ns | 232 B |
| 14 | 43.2 ns | 240 B |
| 15 | 44.8 ns | 240 B |
Parameterless ShortId.Generate() matches the length 15 row above (default options: letters, specials on, numbers off).
Option mix at length 10 (ShortIdOptionsBenchmarks; baseline = letters only):
| Configuration | Mean | Ratio vs letters-only |
|---|---|---|
| Letters only | 26.8 ns | 1.00 |
| Letters + specials | 35.8 ns | 1.34 |
| Letters + numbers | 36.1 ns | 1.35 |
| Letters + numbers + specials | 60.9 ns | 2.28 |
| Sequential (letters + specials) | 78.8 ns | 2.95 |
Takeaways: A minimal character pool and shorter IDs are fastest. Each extra character class adds cost; sequential mode is the slowest option tested here but still sub-microsecond per ID on this machine. Allocation scales roughly with string length (about 208β240 B per ID in the length sweep above, managed heap only).
For speed, prefer a short length and useNumbers: false, useSpecialCharacters: false when you can. For collision resistance at high volume, use at least the default 15 characters and consider including digits; sequential IDs help lexicographic ordering but need enough trailing randomness under burst traffic.
Contributions are welcome.
src/shortid.sln in your IDE, or from the repo root: dotnet build src/shortid.sln.dotnet test src/shortid.Test/shortid.Test.csprojPlease match existing code style and keep public API changes backward compatible unless there is an agreed major version bump.
ShortId is released under the .
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on shortid:
| Package | Downloads |
|---|---|
|
Dmaal.Core.Helper
Package Description |
|
|
Wabbajack.Paths.IO
Package Description |
|
|
kotori-core
Kotori core library. |
|
|
Programatica.Framework.Mvc
Development framework accelarator |
|
|
Yamf.Multitenancy
Package Description |
Showing the top 2 popular GitHub repositories that depend on shortid:
| Repository | Stars |
|---|---|
|
wabbajack-tools/wabbajack
An automated Modlist installer for various games.
|
|
|
replaysMike/Binner
Open source parts inventory system for makers, electronics hobby, and professional engineers
|
| Version | Downloads | Last Updated |
|---|---|---|
| 5.1.1 | 10,969 | 5/26/2026 |
| 5.1.0 | 33,704 | 4/17/2026 |
| 5.0.0 | 45,006 | 3/7/2026 |
| 4.0.0 | 4,476,641 | 4/21/2022 |
| 3.0.2 | 2,334 | 11/5/2023 |
| 3.0.1 | 1,367,030 | 4/11/2021 |
| 3.0.0 | 439,590 | 8/27/2020 |
| 2.0.4 | 46,976 | 8/26/2020 |
| 2.0.3 | 67,801 | 7/4/2020 |
| 2.0.2 | 9,573 | 6/29/2020 |
| 2.0.1 | 258,780 | 3/26/2020 |
| 2.0.0 | 612,235 | 3/9/2018 |
| 1.0.4 | 7,041 | 2/6/2018 |
| 1.0.3 | 12,001 | 11/27/2017 |
| 1.0.2 | 8,225 | 7/5/2017 |
| 1.0.1 | 2,930 | 6/13/2017 |
| 1.0.0 | 4,501 | 6/10/2017 |
- Add project icon