views:

283

answers:

7

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.

+11  A: 

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.

Richard Szalay
+7  A: 

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.

Dan Tao
Totally agree. Classes should always be instantiated in a valid state. If you have a default ctor and set properties post-construction, when is the instance valid? After settings some properties? After setting all? Who knows.
Cumbayah
A: 

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.

Joe
+14  A: 

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()
   {
      // ...
   }
}
Scott Dorman
I think you're missing a few `public` modifiers in there (class members are `private` by default)...
Dan Tao
@Dan Tao: Yes, I was. Thanks @Anthony Pegram for adding them for me.
Scott Dorman
You also shouldn't have () after the class name. Take them out and you've enough code that you can try to compile it and see that this answer is wrong, and attempting to use the default constructor will result in "'Foo' does not contain a constructor that takes '0' arguments".
Jon Hanna
@Jon Hanna: Fixed the () after the class names. However, your comment about the answer being wrong is...well, wrong. The first code will compile and allow you to write code such as `Foo f = new Foo();`. However, as soon as you change the class to that shown in the third example, `Foo f = new Foo();` will cause a compiler error - which was my point. As long as you have *no* constructors, the compiler adds the default constructor for you. As soon as you add *any* other constructor, the compiler no longer adds the default constructor and code which relied on it being present breaks.
Scott Dorman
+1 this. Creating empty default constructors is annoying IMO, and tools like ReSharper just mark it as dead code.
womp
@Scott. Yes, just a mis-read from scanning too quickly. My bad.
Jon Hanna
A: 

Yes generally in most cases it is better to write Default constructor.

Generally if you dont specify any constructor, .NET automatically creates a default constructor for you.

Serialization needs Default constructor.

abhishek
+1  A: 

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.

Jon Hanna
A: 

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.

Øyvind Bråthen