https://github.com/bolorundurowb/shortid
A csharp library to generate short id's. they can be used as primary keys or unique identifiers
https://github.com/bolorundurowb/shortid
csharp id-generation shortid
Last synced: 4 months ago
JSON representation
A csharp library to generate short id's. they can be used as primary keys or unique identifiers
- Host: GitHub
- URL: https://github.com/bolorundurowb/shortid
- Owner: bolorundurowb
- License: mit
- Created: 2017-06-09T09:23:22.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-11-05T01:12:06.000Z (over 2 years ago)
- Last Synced: 2024-10-30T00:35:44.412Z (over 1 year ago)
- Topics: csharp, id-generation, shortid
- Language: C#
- Size: 78.1 KB
- Stars: 230
- Watchers: 9
- Forks: 36
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# ShortID π
[](https://github.com/bolorundurowb/shortid/actions/workflows/build-and-test.yml) [](https://codecov.io/gh/bolorundurowb/shortid) [](LICENSE) 
**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. πͺ
## Getting Started π
### Installation π¦
You can add **ShortId** to your project using one of the following methods:
#### **Package Manager**
```cmd
Install-Package shortid
```
#### **.NET CLI**
```bash
dotnet add package shortid
```
#### **PackageReference**
```xml
```
---
## Usage π οΈ
### Add the Namespace
First, include the **ShortId** namespace in your code:
```csharp
using shortid;
using shortid.Configuration;
```
### Generate a Random ID
To generate a random ID of default length (between 8 and 15 characters), simply call the `Generate` method:
```csharp
string id = ShortId.Generate();
// Example output: KXTR_VzGVUoOY
```
---
### Customize ID Generation π¨
**ShortId** provides several options to tailor the generated IDs to your needs:
#### **Include Numbers**
```csharp
var options = new ShortIdOptions(useNumbers: true);
string id = ShortId.Generate(options);
// Example output: O_bBY-YUkJg
```
#### **Exclude Special Characters**
```csharp
var options = new ShortIdOptions(useSpecialCharacters: false);
string id = ShortId.Generate(options);
// Example output: waBfk3z
```
#### **Specify ID Length**
```csharp
var options = new ShortIdOptions(length: 9);
string id = ShortId.Generate(options);
// Example output: M-snXzBkj
```
#### **Generate Sequential IDs**
For scenarios where you need IDs to be monotonic (i.e., each ID is greater than the previous one), you can enable sequential ID generation:
```csharp
var options = new ShortIdOptions(generateSequential: true);
string id = ShortId.Generate(options);
// Example output: 00000r_v
```
When this option is enabled, the first 6 characters of the ID represent a Base85 encoded timestamp, followed by random characters to complete the requested length.
---
## Customize ShortId ποΈ
**ShortId** allows you to fully customize the character set and even seed the random number generator for reproducible results.
### Change the Character Set
You can define your own character set for ID generation:
```csharp
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.
---
### Set a Random Seed
For reproducible results, you can set a seed for the random number generator:
```csharp
int seed = 1939048828;
ShortId.SetSeed(seed);
```
---
### Reset Customizations
To reset all customizations (character set, seed, etc.) to their defaults, use the `Reset` method:
```csharp
ShortId.Reset();
```
---
## Why Use ShortId? π
- **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.
---
## License π
**ShortId** is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.
---
## Get Started Today! π
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!** π