Code Templates

  • 3 minutes to read

CodeRush includes a large library of code templates in C#, VB, JavaScript, TypeScript, and XAML. Templates are easy-to-memorize character sequences that expand into boilerplate code such as class or property definition. Templates help you write code faster.

You can also create a custom template for any code part in C#, VB, JavaScript, TypeScript, XAML, CSS, HTML, and F# languages. See the following topic for more information on how to create a template: Add a Template.

Expand a Template

Type a template name and press Space to expand the template.

For instance, type "c" and press Space to create a simple class in C#. This creates a new class and prompts you to name it (CodeRush names the constructor automatically).

Templates_Main

You can also expand templates with the Tab key.

Use one of the following ways to bind the "TemplateExpand" command to the Tab key:

  • The Setup Wizard (available from the CodeRush -> Setup Wizard... options menu).

Wizard

  • Shortcuts... items (available from IDE -> Shortcuts options page).

Wizard

The example below shows how to expand the "g3x2" template with the Tab key in XAML to create a grid with three columns and two rows.

Templates_XAML

IMPORTANT

The default CodeRush configuration does not interfere with Visual Studio features such as IntelliSense. IntelliSense prevents the Space or Tab key from expanding templates because this key confirms the IntelliSense suggestion. You should press Esc to close the IntelliSense suggestions window and then press Space to expand a template. You can also force the templates to expand in the IDE | IntelliSense options page. Expand Templates

Expand Templates in Blazor

C# templates can be expanded from @code sections in .razor files.

Blazor

CodeRush declares dependent namespaces when corresponding templates are expanded. For example, the "nsb" template adds the using System.Text namespace reference to the top of a .razor file.

Dependent Namespace Templates

Type Mnemonic

Code templates allow you to declare typed members and variables. For example, you can add a type mnemonic to each member template or variable template to make it expand to the correct type. The screencast below shows how to expand the "ps" template to create a string type property.

PSTemplate

Context-Dependent Templates

CodeRush expands different templates depending on the context. For instance, you can use the "vs" template to create a string type member variable if the caret is inside a class.

VSInClass

or

you can create a local variable of type string if a caret is inside a method.

VSInClass

Template Hints

CodeRush shows hints for some of the more common templates in the Training Window.

Templates

Use the CodeRush | Windows | Training menu item to access the Training Window.

Learn More About Templates

You can find more information on templates in the following topics: