Limiting Class Instances in C# with Private Constructor

Remember how we can create tons of instances of our classes, like making copies of blueprints? Pretty cool, right?

But what if we wanted to limit the number of copies we can make, kinda like having a production cap? Is that even possible?

Absolutely! There is an idea, sirji.

Imagine a counter inside the class, keeping track of how many instances have been created. Whenever someone tries to make a new one, the counter goes up. But if it reaches the limit we set, bam! The gate slams shut, and no more instances can be born.

Okay, we can create a counter inside a class, but how to make this shut to create instances? You know the power of classes with a private constructor. Just think, if a class has a private constructor, how can we create an instance of it? We cannot even inherit it, so it seems useless, right? But nah, still, we can create instances and access them.

If you want to ensure that your class can be instantiated only a limited number of times, such as two times in your case, you can use a combination of a private constructor and a counter variable to track the number of instances created.

Here's an example in C#.

public class LimitedInstancesClass
{
    private static int instanceCount = 0;
    private static readonly int maxInstances = 2;

    // Private constructor
    private LimitedInstancesClass()
    {
        // Increment the instance count 
           when an instance is created
        instanceCount++;
    }

    // Public method to create an instance
    public static LimitedInstancesClass CreateInstance()
    {
             // Check if the maximum number of instances has been reached
        if (instanceCount < maxInstances)
        {
             // If not, create a new instance
            return new LimitedInstancesClass();
        }
        else
        {
             // If the maximum number of instances has been reached, return null or throw an exception
             // You can choose the appropriate behavior based on your requirements
            return null;
             // Alternatively, you can throw an exception to 
             // indicate that no more instances can be created.
             // throw new InvalidOperationException
             //  ("Maximum number of instances reached.");
        }
    }
}

In this example.

  • The constructor is private, so instances can only be created from within the class.
  • The Create Instance method is a public method that checks if the maximum number of instances (maxInstances) has been reached before creating a new instance.
  • The instanceCount variable keeps track of the number of instances created.

You can use this class as follows.

LimitedInstancesClass instance1 = LimitedInstancesClass.CreateInstance();
LimitedInstancesClass instance2 = LimitedInstancesClass.CreateInstance();
LimitedInstancesClass instance3 = LimitedInstancesClass.CreateInstance(); 

// This will return null or throw an exception after the maximum number of instances is reached

This approach allows you to control the number of instances created and enforce the limitation you specified.


Recommended Free Ebook
Similar Articles