How can I force the base constructor to be called in C#?

I have a BasePage class which all other pages derive from:

public class BasePage

This BasePage has a constructor which contains code which must always run:

public BasePage()
    // Important code here

I want to force derived classes to call the base constructor, like so:

public MyPage
    : base()
    // Page specific code here

How can I enforce this (preferably at compile time)?


The base constructor will always be called at some point. If you call this(...) instead of base(...) then that calls into another constructor in the same class - which again will have to either call yet another sibling constructor or a parent constructor. Sooner or later you will always get to a constructor which either calls base(...) explicitly or implicitly calls a parameterless constructor of the base class.

See this article for more about constructor chaining, including the execution points of the various bits (such as variable initializers).

Need Your Help

How to set property in IDataErrorInfo validator from Xaml

c# wpf validation xaml idataerrorinfo

When using IDataErrorInfo in WPF is there a way to pass parameters to the validator. For instance I have a DueDate Datepicker. When validating for a new task I want to restrict the date allowed to ...

How to change the resolution time calculation in JIRA?

project-management jira kanban

Is it possible to change the resolution time calculation to start not with the issue creation time, but rather with the time when an issue was transferred into a certain state?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.