DateTime.Now.Ticks repeating inside a loop

前端 未结 6 1171
小鲜肉
小鲜肉 2021-01-27 02:55

I am trying to generate a unique ID for the primary key of a table and I am using DateTime.Now.Ticks for it. That\'s a requirement for now we can\'t use Ident

6条回答
  •  长发绾君心
    2021-01-27 03:23

    TL;DR:

    The precision of DateTime.Now is around 15ms, if you loop faster than that, DateTime.Now will have the same value over differents iterations.


    As described in the .Net source code:

    The data is stored as an unsigned 64-bit integeter

    Bits 01-62: The value of 100-nanosecond ticks where 0 represents 1/1/0001 12:00am, up until the value 12/31/9999 23:59:59.9999999

    Furthermore, the DateTime.Now is quite imprecise:

    The resolution of this property depends on the system timer, which is approximately 15 milliseconds on Windows systems


    For the ID assignation part, as other stated, use GUID which have been made for this purpose.

提交回复
热议问题