Constructors for Abstract Types

Problem
You want to know how to implement constructors in abstract types.

Solution
Assign a protected scope to constructors for abstract classes.

Comments
Give constructors in abstract classes protected visibility since they are intended only to be called by derived types. The C# compiler will automatically create a protected constructor in abstract classes for you. However, it’s recommended that you explicitly define one.

Example

// OK. The default constructor is appropriately handled by the compiler.
public abstract class File {
    ...
}

// The recommended approach. The constructor is explicitly defined.
public abstract class File {
    // Leave the constructor empty.
    protected File() {
    }
    ...
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s