Smart Constructor – Generating advanced type constructors in three keystrokes
The Smart Constructor CodeRush feature allows you to add constructors to the current class or structure, and pass type members, like fields and properties for initialization through its parameters. Creating a constructor is as simple as just typing three keys: “cc & Space”:
The Smart Constructor feature is based on the CodeRush Templates Engine, that’s why it is so simple to execute. The “cc” code template is specially dedicated to this feature, and the Space key simply expands the template, by default. The template mnemonic (cc) is easy to learn, when you know that it means the “Create Constructor” (cc).
If the active type has members, such as fields and/or auto-implemented properties, the Smart Constructor allows you to choose any of them for initialization inside the constructor, using the “Parameters to the constructor” dialog, for example:
You can use the mouse cursor or the Space key to check or uncheck the object in the list. When you are done with selecting members, press the Enter key to commit your changes or the Esc key, if you changed you mind on creating a new constructor. Those items that have a check will be initialized through parameters to the constructor when committed, for example:
By default, Smart Constructor generates an XML doc comment with a standard summary description, which is configurable, of course. All settings for the feature are available on the Editor | Text Commands | Smart Constructor options page in the IDE Tools Options Dialog:
Here, on this page, you can specify the following options:
- Whether to show the “Parameters to the constructor” dialog
- Show always
- Show only when there are more than [X] members, where you can choose X number
- Never show
- The sort mode inside the “Parameters to the constructor” dialog:
- By declaration position will list member in the order as they appear in the code
- By name
- By type will sort member by its type (e.g. String, Int32, etc)
- Whether to generate XML doc comment and its default summary description
- Whether to generate default constructor for serializable types, for example:
- Whether to generate read-only properties for the selected private fields, for example:
- Whether public fields from the base type should be listed in the “Parameters to the constructor”, for example: