8

i can't understand the difference between a simple bare

Public ClassName() {}

and

Public ClassName() : this(null) {}

I know I can use it only if i have a +1 overloaded ctor, but I can't understand the advantages of defining the parameterless constructor this way.

Eric Andres
  • 3,417
  • 2
  • 24
  • 40
Sergio
  • 2,078
  • 3
  • 24
  • 41

3 Answers3

10

This permits the single-param constructor to have all the logic, so it isn't repeated.

public ClassName() : this(null) {}

public ClassName(string s)
{
    // logic (code)
    if (s != null) {
        // more logic
    }
    // Even more logic
}

I hope it's clear that the "logic" and "even more logic" would have needed to be repeated in the parameterless constructor if not for the this(null).

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Thank you for replying. What do you mean with "having all the logic" exactly? – Sergio Feb 13 '13 at 18:54
  • 1
    @Daedalus Say you need to do some setup work in the constructor that does not depend at all on the parameter. Instead of copy and pasting the code twice you can just have the paramtetrless constructor call the 2nd constructor and put the setup work once there. – Scott Chamberlain Feb 13 '13 at 18:57
  • 1
    @Daedalus Rather than repeating the code in both constructors with a minor change for the existence of one parameter, you put all of the code in one constructor and have the other one call the one with all of the code and some default values. – Servy Feb 13 '13 at 18:57
3

One very useful case is situations like WinForms where the designer requires a prameterless constructor but you want your form to require a constructor.

public partial SomeForm : Form
{
    private SomeForm() : this(null)
    {
    }

    public SomeForm(SomeClass initData)
    {
        InitializeComponent();

        //Do some work here that does not rely on initData.           

        if(initData != null)
        {
           //do somtehing with initData, this section would be skipped over by the winforms designer.
        } 
    }
}
Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431
  • Ok but if I omit the this(null) nothing changes, at my eyes... What am I missing? – Sergio Feb 13 '13 at 18:57
  • 2
    No, if you omit `this(null)` the designer will never call `InitializeComponent()` – Scott Chamberlain Feb 13 '13 at 18:58
  • @ScottChamberlain But it's an unused constructor, since, as you say, the parameter is required, the parameterless constructor will never be used; it's just there to make it compile. – Servy Feb 13 '13 at 18:58
  • 5
    @Servy But it is used, by the winforms designer. if you omit the constructor you can not use the designer. – Scott Chamberlain Feb 13 '13 at 18:59
  • I have forms in my current project that have no parameterless constructor, and they do work with the designer. The designer doesn't actually instantiate your form; it instantiates the base `Form` class. [More Info](http://blogs.msdn.com/b/rprabhu/archive/2004/12/12/280823.aspx). – JosephHirn Feb 13 '13 at 19:15
1

There is a pattern called Constructor injection. This pattern is mainly useful for unit testing and sharing the logic. Here is an example

public class SomeClass 
{
   private ISomeInterface _someInterface;
   public SomeClass() : this (null){} //here mostly we pass concrete implementation 
   //of the interface like this( new SomeImplementation())

   public SomeClass(ISomeInterface someInterface)
   {
      _someInterface = someInterface;       
      //Do other logics here
   }
}

As you see here, unit tests will be easy by passing fake implementation. In addition, the logic is shared ( DRY). And Do all the logic inside the constructor which takes the highest number of parameters

But in your case, null is passing, so that is a context based. I have to know what your context is.

Guilherme Oliveira
  • 2,008
  • 3
  • 27
  • 44
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38
  • Constructor Injection is used with Dependency Injection. The question and your answer are both about Constructor Chaining. – Metro Smurf Feb 13 '13 at 19:00