shortid 5.1.0
dotnet add package shortid --version 5.1.0
NuGet\Install-Package shortid -Version 5.1.0
<PackageReference Include="shortid" Version="5.1.0" />
<PackageVersion Include="shortid" Version="5.1.0" />
<PackageReference Include="shortid" />
paket add shortid --version 5.1.0
#r "nuget: shortid, 5.1.0"
#:package shortid@5.1.0
#addin nuget:?package=shortid&version=5.1.0
#tool nuget:?package=shortid&version=5.1.0
ShortID π
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.
Installation
Package Manager
Install-Package shortid
.NET CLI
dotnet add package shortid
PackageReference (pin the version you want from NuGet)
<PackageReference Include="shortid" Version="5.0.0" />
Usage
Add the namespace:
using shortid;
Basic generation
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 is 15 characters unless you pass a different
lengthinShortIdOptions. - Special characters (
_and-) may appear; numbers are off by default. - IDs are not sequential unless you opt in.
Options
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.
Global customisation
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.
Exceptions
Generate(null)throwsArgumentNullException.lengthbelow 8 throwsArgumentException.- Invalid
SetCharactersinput throwsArgumentExceptionorInvalidOperationExceptionas documented in code.
Benchmarks
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 --.
Reference run (April 2026)
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.
Contributing
Contributions are welcome.
- Issues and pull requests: Open an issue to discuss larger changes; pull requests should stay focused and include tests when behavior changes.
- Build: Open
src/shortid.slnin your IDE, or from the repo root:dotnet build src/shortid.sln. - Tests:
dotnet test src/shortid.Test/shortid.Test.csproj - Benchmarks: See Benchmarks; use Release configuration for meaningful results.
Please match existing code style and keep public API changes backward compatible unless there is an agreed major version bump.
License
ShortId is released under the MIT License.
| Product | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.3)
NuGet packages (16)
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 |
GitHub repositories (2)
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.0 | 14 | 4/17/2026 |
| 5.0.0 | 23,676 | 3/7/2026 |
| 4.0.0 | 4,187,572 | 4/21/2022 |
| 3.0.2 | 2,226 | 11/5/2023 |
| 3.0.1 | 1,313,306 | 4/11/2021 |
| 3.0.0 | 429,558 | 8/27/2020 |
| 2.0.4 | 46,438 | 8/26/2020 |
| 2.0.3 | 67,615 | 7/4/2020 |
| 2.0.2 | 9,464 | 6/29/2020 |
| 2.0.1 | 232,990 | 3/26/2020 |
| 2.0.0 | 597,981 | 3/9/2018 |
| 1.0.4 | 6,928 | 2/6/2018 |
| 1.0.3 | 11,978 | 11/27/2017 |
| 1.0.2 | 8,204 | 7/5/2017 |
| 1.0.1 | 2,912 | 6/13/2017 |
| 1.0.0 | 4,434 | 6/10/2017 |
Documentation
- README reworked for library users: clearer sections, fewer decorative elements, contributing and benchmark notes aligned with the repo.
Behavior
- Default ID length is now fixed at 15 characters when no length is passed (previously a random length between 10 and 14). Callers that assumed variable default length should set an explicit length if they need the old behavior.
Performance
- Parameterless ShortId.Generate() reuses a single default ShortIdOptions instance instead of allocating one per call.
Internals
- Default length is exposed as Constants.DefaultOutputLength (15). Random-length helpers for the old default were removed; CommonUtilities.GenerateNumberInRange was dropped as unused.