Should we always include a default constructor in the class?

C#.NetClrDefault Constructor

C# Problem Overview


I have been asked this question by a colleague that should we always include a default constructor in a class? If so, why? If no, why not?

Example

public class Foo {

    Foo() { }

    Foo(int x, int y) {
        ...
    } 

}

I am also interested to get some lights on this from experts.

C# Solutions


Solution 1 - C#

You have to keep in mind that if you don't provide an overloaded constructor, the compiler will generate a default constructor for you. That means, if you just have

public class Foo
{ 
} 

The compiler will generate this as:

public class Foo
{ 
    public Foo() { }  
} 

However, as soon as you add the other constructor

public class Foo
{ 
    public Foo(int x, int y)
    { 
        // ... 
    }  
} 

The compiler will no longer automatically generate the default constructor for you. If the class was already being used in other code which relied on the presence of a default constructor, Foo f = new Foo();, that code would now break.

If you don't want someone to be able to initialize the class without providing data you should create a default constructor which is private to be explicit about the fact that you are preventing instances from being constructed with no input data.

There are times, however, when it is necessary to provide a default constructor (whether public or private). As was previously mentioned, some types of serialization require a default constructor. There are also times when a class has multiple parameterized constructors but also requires "lower level" initialization, in which case a private default constructor can be used which is chained in from the parameterized constructors.

public class Foo
{
   private Foo()
   {
      // do some low level initialization here
   }

   public Foo(int x, int y)
      : this()
   {
      // ...
   }

   public Foo(int x, int y, int z)
      : this()
   {
      // ...
   }
}

Solution 2 - C#

Some things (like serialisation) require a default constructor. Outside of that, however, a default constructor should only be added if it makes sense.

For example, if the Foo.X and Foo.Y properties are immutable after construction then a default constructor doesn't really make sense. Even if it were to used for an 'empty' Foo, a static Empty accessor would be more discoverable.

Solution 3 - C#

I would say no, definitely not always. Suppose you have a class with some readonly fields that must be initialized to some value, and there are no reasonable defaults (or you don't want there to be)? In this scenario I don't think a parameterless constructor makes sense.

Solution 4 - C#

Having a default constructor is only a good idea if it makes sense to have such an object.

If you produce an object which is not in a valid state from such a constructor, then the only thing it can do is introduce a bug.

Solution 5 - C#

As a side note, when using struct instead of class, note that there are no way to leave the default constructor out, nor is it possible to define it yourself, so regardless of which constructors you define, make sure that the default state of the struct (when all variables are set to their default state (usually 0 for value types, and null for reference types) won't break your implementation of the struct.

Solution 6 - C#

Yes It is better to have a default constructor in place to ensure you avoid any confusion. I have seen people just do nothing inside a default constructor (even in Microsoft's own classes) but still like to keep it as objects do get default(type) automatically. The classes which does not specify default constructor, .NET will automatically add them for you.

Serialization needs Default constructor if you use existing serializers as it makes sense for general purpose serializers, otherwise you need to create your own implementation.

Solution 7 - C#

In most cases a default constructor is a good idea. But since you use the word "always", all that's needed is one counterexample. If you look at the Framework, you'll find plenty. For example, System.Web.HttpContext.

Solution 8 - C#

A generic type can only be instantiated with C# means (without reflection) if it has a default constructor. Also, the new() generic type constraint has to be specified:

void Construct<T>()
    where T : new()
{
    var t = new T();
    ...
}

Calling this method using a type as generic type argument that has no default constructor results in a compiler error.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMoonView Question on Stackoverflow
Solution 1 - C#Scott DormanView Answer on Stackoverflow
Solution 2 - C#Richard SzalayView Answer on Stackoverflow
Solution 3 - C#Dan TaoView Answer on Stackoverflow
Solution 4 - C#Jon HannaView Answer on Stackoverflow
Solution 5 - C#Øyvind BråthenView Answer on Stackoverflow
Solution 6 - C#abhishekView Answer on Stackoverflow
Solution 7 - C#JoeView Answer on Stackoverflow
Solution 8 - C#Olivier Jacot-DescombesView Answer on Stackoverflow