21
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ulid の C# 実装 NUlid を使ってみた

Last updated at Posted at 2019-01-09

ulid とは

【Qiita】ソート可能なUUID互換のulidが便利そうという記事で紹介されています。時系列でソートされる UUID のようです。

NUlid とは

【Github】RobThree/NUlid

C# で実装された ulid ライブラリです。Ulid 型はタイムスタンプとランダム値で表される値で、Time プロパティから生成日時(UTC)を取得することができました。
重複しないID値をキーにしたいが、生成順にソートできるようにもしたいというようなケースで利用できそうです。

// 新規IDを生成
NUlid.Ulid id = NUlid.Ulid.NewUlid();

Debug.WriteLine(id.ToString());
Debug.WriteLine(id.Time);
Debug.WriteLine(id.Time.ToLocalTime());
Debug.WriteLine(BitConverter.ToString(id.Random));
Debug.WriteLine(id.ToGuid());
出力結果
01D0SM8S9X08NG5YMG4C79CN7P
2019/01/09 15:39:06 +00:00
2019/01/10 0:39:06 +09:00
02-2B-02-FA-90-23-0E-96-54-F6
44336801-3d65-2b02-02fa-90230e9654f6
// 10個の値を生成
for (int i=0; i< 10; ++i)
{
    Debug.WriteLine(NUlid.Ulid.NewUlid());
}
出力結果
01D0SMPX6A58EZR2KYFGCK3H81
01D0SMPX6B7M7G8BVGY5R44BFE
01D0SMPX6CEB94VCPWC9YX3F1Z
01D0SMPX6CESDT8N2AJWRAXZSP
01D0SMPX6D8VYKZ19M71NWFNY0
01D0SMPX6D5814C2Y0KAS6FK9T
01D0SMPX6EQAWGM9NWZQQ4TNQZ
01D0SMPX6FZAAHP2ZBQCZ29B5Z
01D0SMPX6F7R9QJGD50YX38VKC
01D0SMPX6GHWKSRXCFMCF264H0
21
28
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
28

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?