C# 生成订单号的几种方式

    public class RandomNumber
    {
        public static object _lock = new object();
        public static int count = 1;

        public string GetRandom1()
        {
            lock(_lock)
            {
                if(count >= 10000)
                {
                    count = 1;
                }
                var number = "P" + DateTime.Now.ToString("yyMMddHHmmss") + count.ToString("0000");
                count++;
                return number;
            }
        }


        public string GetRandom2()
        {
            lock(_lock)
            {
                return "T" + DateTime.Now.Ticks;

            }
        }

        public string GetRandom3()
        {
            lock(_lock)
            {
                Random ran = new Random();
                return "U" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ran.Next(1000, 9999).ToString();
            }
        }
    }

调用:

  Response.Write("Number1:"+new RandomNumber().GetRandom1());

  Response.Write("     Number2:" + new RandomNumber().GetRandom2());

  Response.Write("     Number3:" + new RandomNumber().GetRandom3());

结果:
Number1:P1608171449140001
Number2:T636070421545568764
Number3:U201608171449145562080

你可能感兴趣的:(C#技术类)