C# 生成订单号的几种方式
1 public class RandomNumber 2 { 3 public static object _lock = new object(); 4 public static int count = 1; 5 6 public string GetRandom1() 7 { 8 lock(_lock) 9 {10 if(count >= 10000)11 {12 count = 1;13 }14 var number = 'P' DateTime.Now.ToString('yyMMddHHmmss') count.ToString('0000');15 count ;16 return number;17 }18 }19 20 21 public string GetRandom2()22 {23 lock(_lock)24 {25 return 'T' DateTime.Now.Ticks;26 27 }28 }29 30 public string GetRandom3()31 {32 lock(_lock)33 {34 Random ran = new Random();35 return 'U' DateTime.Now.ToString('yyyyMMddHHmmssfff') ran.Next(1000, 9999).ToString();36 }37 }38 }
调用:
Response.Write('Number1:' new RandomNumber().GetRandom1()); Response.Write('Number2:' new RandomNumber().GetRandom2()); Response.Write('Number3:' new RandomNumber().GetRandom3());
结果:
Number1:P1608171449140001
Number2:T636070421545568764
Number3:U201608171449145562080
赞 (0)