#294 – Make All Constructors Private to Prevent Object Creation

If you want to prevent code external to a class from creating instances of that class, you can make all of the constructors of the class private.

In the following example, we have a single Dog constructor, which is private.

        private Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }

Because the constructor is private, code outside the Dog class cannot create a new instance of a Dog.

But we can have a static method in the Dog class that can create instances.  Because the code is defined inside the Dog class, it has access to the private constructor.

public class Dog
{
        // code omitted

        public static Dog MakeADog()
        {
            // Use private constructor
            Dog nextDog = new Dog(nameList[nextDogIndex], ageList[nextDogIndex]);

            nextDogIndex = (nextDogIndex == (nameList.Length - 1)) ? 0 : nextDogIndex++;

            return nextDog;
        }

Now if we want a new Dog instance, we can call the MakeADog method.

#293 – You Can Declare a Private Constructor

It’s possible to have one or more instance constructors be private.  A private constructor means that only code internal to the class can construct an instance using that particular combination of parameters.

Below is an example, where we have a public Dog constructor that takes two arguments, but a private one that takes only a name.

        public string Name { get; set; }
        public int Age { get; set; }

        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }

        private Dog(string name)
        {
            Name = name;
        }

A private constructor is typically called from within a static method in the class. For example:

        public static Dog MakeAnOldDog()
        {
            // Use private constructor
            Dog oldDog = new Dog("Rasputin");
            oldDog.Age = 15;

            return oldDog;
        }

#291 – No Default Constructor if You Define any Constructors

If you don’t define any constructors in a class, the compiler automatically generates a default parameterless constructor.  You can then create a new instance of the object without passing any parameters.

            Dog d1 = new Dog();

If you define at least one constructor, the compiler no longer generates a parameterless constructor.

Below, we define a single constructor for Dog, accepting a name parameter.

    public class Dog
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Dog(string name)
        {
            Name = name;
            Age = 1;
        }
    }

Now we can define a new instance of Dog by passing in a name parameter.  But we can no longer create a new instance using no parameters.

            Dog d1 = new Dog("Rin Tin Tin");

            // Compiler error: Dog does not contain a constructor
            //   that takes 0 arguments.
            Dog d2 = new Dog();

#290 – Chaining Constructors

You can have one constructor in a class call another.  This is known as constructor chaining.

With constructor chaining, one constructor calls another to help it initialize the data in the class.

As an example, assume that we have a Dog class with two constructors, one which accepts name and age parameters, and one which accepts only a name parameter.

In the example below, the constructor that takes two arguments initializes both name and age properties.  The constructor that takes only a name parameter chains to the first constructor, passing it the name parameter and a default value for age.  This first constructor is called using the this keyword.

    public class Dog
    {
        public string Name { get; set; }
        public int Age { get; set; }

        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }

        public Dog(string name)
            : this(name, 1)
        {
        }
    }

#289 – You Can Define Multiple Constructors

We can define multiple constructors in a class,  each one taking a different set of parameters.

Here’s an example where we define four different constructors for a Dog object.

        public string Name { get; set; }
        public int Age { get; set; }
        public string Motto { get; set; }

        public Dog(string name)
        {
            Name = name;
            Age = 1;
            Motto = "Happy";
        }

        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
            Motto = "Happy";
        }

        public Dog(string name, string motto)
        {
            Name = name;
            Motto = motto;
            Age = 1;
        }

        public Dog(string name, int age, string motto)
        {
            Name = name;
            Age = age;
            Motto = motto;
        }

We now have four different ways to construct a Dog object.

            Dog d1 = new Dog("Kirby");                        // name
            Dog d2 = new Dog("Jack", 16);                     // name, age
            Dog d3 = new Dog("Ruby", "Look out window");      // name, motto
            Dog d4 = new Dog("Lassie", 71, "Rescue people");  // name, age, motto

#288 – Passing Arguments to a Constructor

You can define a constructor in a class that takes one or more arguments.  Typically, these represent data to be used in initializing the new object.

Here’s an example of a constructor for the Dog class that accepts the dog’s name and age and then assigns those values to the corresponding properties.

        public string Name { get; set; }
        public int Age { get; set; }

        // Constructor that takes dog's name and age
        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }

Having this constructor, we can instantiate a new Dog instance as follows:

            Dog kirby = new Dog("Kirby", 14);

#287 – You Don’t Have to Define a Constructor

It’s not mandatory for a user-defined class to define a constructor.  If one is not defined, the compiler will automatically generate a constructor. This internal constructor will just call the constructor of the class’ base class.  (E.g. The constructor in System.Object).

You can use the IL DASM tool to inspect the code for your class and see this automatically generated constructor.

For example, let’s say that we have a Dog class that does not define a constructor.  Below is an image of the IL DASM, showing the metadata for the Dog class.  Note that it shows the following elements in the class:

  • Age and Name properties
  • get and set accessors for the properties
  • Backing variables for the properties
  • A Bark method
  • A method called .ctor–this is the automatically-generated constructor

This constructor just calls the System.Object constructor: