Should the window's public default constructor be?

I am working on a dialog box for my WPF application. I know WPF requires all controls to have a default constructor, and I can create all constructors that take whatever parameters I want. But should there be a public default constructor? Can I make it internal, or private, or even secure?

+3


source to share


2 answers


Controls don't need a default constructor in WPF unless you want to instantiate the control from XAML.

It is perfectly legal to have a control like this:

public partial class MyUserControl : UserControl
{
      public MyUserControl(string someParameter) : this()
      { 
         InitializeComponent();
      }
}

      



As shown, the default constructor has been removed, you need to make sure it InitializeComponent();

gets called.

If you instantiate a control from XAML, the default constructor must be visible to the attachment control

So it might be internal

if the embed control is in the same assembly, otherwise it should be public

otherwise.

+6


source


creating an internal constructor ensures that the type will only ever be created by types within the current assembly, even if it is later decided that the type itself should be public rather than internal. In other words, you can decide to change the type's visibility without limiting the constraints on its creation.



Making a public constructor has the opposite effect (obviously) and might be smart if you want it to be able to instantiate a type wherever it is visible.

+1


source







All Articles