Empty constructor or no constructor

I think it is not mandatory to have a default constructor in a class (C#).

So, in that situation shall I have an empty constructor in the class or can I skip it?

Is it a best practice to have an empty default constructor?

Class test
{
   public test()
   {

   }
        ......
}

or

Class test
{
        ......
}

If the class won't be used by third parties and you don't need an overloaded constructor, don't write an empty constructor.

But...

Imagine you already shipped the product, and third parties use your class. A few months later there's a new requirement that makes you add a constructor with an argument.

Now, by doing so, the C# compiler no longer generates a default constructor. If you don't add an empty constructor explicitly, the third party code will be break.

In my opinion, you should always define empty constructors (one liner) for public classes used by third parties.


KISS - If your class doesn't need to do anything in the default constructor - don't define it. The compiler will generate one for you.

From the horse's mouth:

Unless the class is static, classes without constructors are given a public default constructor by the C# compiler in order to enable class instantiation.


There's nothing like default constructors. It's public parameter-less constructor in C#. C# compiler will look for constructors in a class while compiling and will add a public parameter-less constructor if no constructors are defined. So it's ok to skip defining a constructor if your class objects don't need any special operation for construction.

Secondly, if you define one constructor with parameter then compiler will not add a public parameter-less constructor because compiler will assume your objects must be constructed via the constructor you've defined. Having said that, you must explicitly define a parameter-less constructor if you have at least one parameterized constructor, if you need your objects to construct with parameteress constructor.

You can also have a private parameterless constructor as well that is instrumental in making singleton classes.


If you don't need a parameterless constructor, don't add one with an empty body. However, if someone wants to serialize/deserialize objects of your class, then you'll need a parameterless constructor.

If you define no constructors, then the compiler will automatically generate a default parameterless one for you, but it won't if you define a constructor yourself.

Incidentally, you can suppress the C# compiler's automatically generated default constructor by defining an empty, private constructor.