Guid.NewGuid() VS a random string generator from Random.Next()

后端 未结 7 1651
攒了一身酷
攒了一身酷 2021-02-01 17:51

My colleague and I are debating which of these methods to use for auto generating user ID\'s and post ID\'s for identification in the database:

One option uses a single

7条回答
  •  太阳男子
    2021-02-01 17:54

    Use System.Guid as it:

    ...can be used across all computers and networks wherever a unique identifier is required.

    Note that Random is a pseudo-random number generator. It is not truly random, nor unique. It has only 32-bits of value to work with, compared to the 128-bit GUID.

    However, even GUIDs can have collisions (although the chances are really slim), so you should use the database's own features to give you a unique identifier (e.g. the autoincrement ID column). Also, you cannot easily turn a GUID into a 4 or 20 (alpha)numeric number.

提交回复
热议问题