C# Class Auto increment ID

前端 未结 6 1350
别那么骄傲
别那么骄傲 2021-02-03 12:05

I am creating a class in C# called \"Robot\", and each robot requires a unique ID property which gives themselves an identity.

Is there any way of creating an auto incr

6条回答
  •  时光说笑
    2021-02-03 12:46

    public static void beAddedTo(this T item, Dictionary dic) where T : m.lib.RandId
    {
        Random ran = new Random();
        var ri = ran.Next();
        while (Program.DB.Rooms.ContainsKey(ri)) ri = ran.Next();
        item.Id = ri;
        dic.Add(item.Id, item);
    }
    

    Not incremental but you can add and delete item how many time you want. (Maximum item should be lower than int.Max/2)

提交回复
热议问题