Limit instances creation of a class?

前端 未结 6 1645
暖寄归人
暖寄归人 2021-01-11 16:15

I am using C#. I have created a class which can be included in any c#.net project (desktop or web based), but I want that only 10 objects will be created in that application

6条回答
  •  滥情空心
    2021-01-11 16:49

    I would create a static integer and update it when you instantiate a new object.

    class YourClass
    {
        static int Count = 0;
    
        public YourClass()
        {
           Count++;
           if(Count > 10)
           {
               //throw exception
           }
        }
    }
    

提交回复
热议问题