How to Generate Unique Number of 8 digits?

后端 未结 9 984
庸人自扰
庸人自扰 2020-12-30 01:39

I am using this code to generate a 8 digit unique number.

byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToUInt32(buffer, 8).ToString();
<         


        
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-30 02:18

    My first answer did not address the uniqueness problem. My second one does:

    static int counter;
    public static int GetUniqueNumber()
    { 
        return counter++; 
    }
    

    If you want to have unique numbers across app restarts, you need to persist the value of counter to a database or somewhere else after each and every GetUniqueNumber call.

提交回复
热议问题