Skip to main content

Code Templates

  • 4 minutes to read

CodeRush includes a large library of code templates in C#, Visual Basic, JavaScript, TypeScript, HTML, Razor, 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#, Visual Basic, JavaScript, TypeScript, XAML, CSS, HTML, Razor, and F# languages. See the following topic for more information on how to create a template: Add a Template.

Template List

The following topics describe the main CodeRush templates:

To see the full template list, perform the following actions:

Choose the CodeRush | Code Templates… menu item.

Templ_Config_path

Note

CodeRush menu is placed in Visual Studio Extensions menu for Visual Studio 2019 and Visual Studio 2022 Preview. You can restore the default CodeRush menu position in Visual Studio 2019 16.4 or later. See the following topic for more information: First Steps.

Open the Editor | All Languages | Templates options page.

templates-page

Choose the desired language in the “Language” combo box. You can use the search text box to find the required template.

Template category

For more information about templates options page, see the following topic: Template Configuration.

If you prefer to have a printable copy of CodeRush code templates, download CodeRush Cheat Sheet.

CodeRush also shows information about templates that are available in the current context in the Training window.

training-window

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

training-menu-item

For more information about the Training window, refer to the following topic: Training Window.

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

The Shortcuts… options page (available from the CodeRush -> Shortcuts… options menu).

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

Expand a Template Instead of Visual Studio IntelliSense

Visual Studio IntelliSense has priority over CodeRush templates. IntelliSense prevents the Space or Tab key from expanding templates because this key confirms the IntelliSense suggestion.

Press Esc to close the IntelliSense suggestions window and then press Space to expand a CodeRush template.

Dependent Namespace Templates

You can also enable the “Always expand templates” option on the IDE | IntelliSense options page to prioritize CodeRush template over Visual Studio IntelliSense.

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 in the following cases:

Create a string type member variable if the caret is inside a class.

VSInClass

Create a local variable of the type string if a caret is inside a method.

VSInClass

Member Sections

The Member Sections feature can place properties, methods, and fields in dedicated target sections when you expand templates.

PlaceMembers

If you want to disable this feature, uncheck the Use Member Sections checkbox in the Editor | All Languages | Organize Members options page.

disable-selection

For more information, see the following topic: Member Sections.

Learn More About Templates

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