ShortId is a lightweight and efficient C# library designed to generate completely random, short, and unique identifiers. These IDs are perfect for use as primary keys, unique identifiers, or any scenario where you need a compact, random string. 🎯
What sets ShortId apart is its flexibility—you can specify the length of the IDs (between 8 and 15 characters) and customize the character set. It’s also thread-safe, making it ideal for high-performance applications that require generating millions of unique IDs across multiple threads. 💪
You can add ShortId to your project using one of the following methods:
Install-Package shortid
dotnet add package shortid
<PackageReference Include="shortid" />
First, include the ShortId namespace in your code:
using shortid;
using shortid.Configuration;
To generate a random ID of default length (between 8 and 15 characters), simply call the Generate
method:
string id = ShortId.Generate();
// Example output: KXTR_VzGVUoOY
ShortId provides several options to tailor the generated IDs to your needs:
var options = new GenerationOptions(useNumbers: true);
string id = ShortId.Generate(options);
// Example output: O_bBY-YUkJg
var options = new GenerationOptions(useSpecialCharacters: false);
string id = ShortId.Generate(options);
// Example output: waBfk3z
var options = new GenerationOptions(length: 9);
string id = ShortId.Generate(options);
// Example output: M-snXzBkj
ShortId allows you to fully customize the character set and even seed the random number generator for reproducible results.
You can define your own character set for ID generation:
string characters = "ⒶⒷⒸⒹⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙⓚⓛⓜⓝⓞⓟⓠⓡⓢⓣⓤⓥⓦⓧⓨⓩ①②③④⑤⑥⑦⑧⑨⑩⑪⑫"; // Custom character set
ShortId.SetCharacters(characters);
Note:
- The character set must contain at least 50 unique characters.
- Duplicate and whitespace characters are automatically removed.
For reproducible results, you can set a seed for the random number generator:
int seed = 1939048828;
ShortId.SetSeed(seed);
To reset all customizations (character set, seed, etc.) to their defaults, use the Reset
method:
ShortId.Reset();
- Flexible Length: Generate IDs between 8 and 15 characters long.
- Customizable: Use your own character set or exclude special characters.
- Thread-Safe: Perfect for multi-threaded applications.
- Lightweight: Minimal overhead, maximum performance.
- Easy to Use: Simple API with just a few methods.
ShortId is licensed under the MIT License. See the LICENSE file for more details.
Whether you need unique IDs for database keys, URLs, or any other purpose, ShortId has you covered. Install the package, follow the examples, and start generating unique IDs in seconds! ⏱️
Happy Coding! 🚀