CHAPTER 6
Just like with refactorings, navigation, and other feature-rich menus, Rider provides a single Generate menu (Alt+Insert in VS layout) that offers you a selection of code generation options for creating common boilerplate code. Here’s a selection:

Figure 69
Let’s discuss some of the options in more detail:
Most of these menu items actually show a dialog box that lets you fine-tune the code generation process to your liking:

Figure 70
In Figure 70, you can see that, in addition to choosing the members to be used for comparisons, you also have the option of overloading equality operators (= and !=) or implementing IEquatable<T> , and there’s even a choice of how the types of different variables are compared for type-safe equality comparisons.
It’s worth noting that Rider does not ensure post-hoc consistency of the generated code. This means that if your underlying structures change, the implementation of the generated structures might need to be manually regenerated.
The Generate menu is all well and good, but sometimes you want to generate code without breaking your workflow. To help with this, Rider builds a few refactorings right into code completion.
Take constructors, for example. In addition to using the Generate menu, Rider provides shortcuts for generating a fully initializing constructor that initializes all the fields (ctorf), all the properties (ctorp), or all fields and properties (ctorfp).

Figure 71
If we choose the ctorfp option shown in Figure 71 our code will look like this:
Code Listing 3
Another piece of generative completion is the automatic creation of overrides and properties for associated fields. Simply start typing the name of an override, and you’ll get code completion for finishing the override signature:

Figure 72
Same goes for properties: given a field, start typing the corresponding property name, and you’ll be presented with the following option:

Figure 73
As you may have guessed, these options let you generate either a read-write or a read-only property for the field.
Postfix completion is a form of post-hoc completion, that is, completion that shows up as available on a trailing dot (period) on symbols. It adds further code completion elements that allow you to perform common actions on the completion subject.
For example, given an ordinary variable, you’ll see completion items such as null or notnull:

Figure 74
Invoking this notnull completion transforms the use of the variable to an if statement containing the null check being performed on the variable:
Code Listing 4
The typical development workflow is that you first make classes, and then you use them. But the Create from Usage mechanic allows you to do the opposite—to use fields, methods, and entire classes that have not been declared, and then use Rider’s quick-fix options to generate the appropriate code constructs.
Here’s a small example. Imagine that I write the following:
Code Listing 5
var p = new Person(); p.Name = "Dmitri"; |
Assuming that the Person class does not exist, the identifier will be shown in red. With the caret over the offending (red) symbol, I can pres Alt+Enter and be presented with the following options:

Figure 75
The first two options allow you to automatically create a type called Person as a top-level class or as a nested class within the current class. The last option, Change all, allows you to rename all locations of Person found in your code to something else; this is useful, for example, when you’re importing code that relies on a type, but in your own code that type has been renamed.
Create from Usage is very smart. In most cases, it will try to guess the expected types. For example, in the statement p.name = “Dmitri”, Rider is offering to generate a field or a property:

Figure 76
By default, Rider is assuming that we want a property; it gets this idea due to the naming conventions we’ve set (properties start with a capital letter). It does, however, know that we might want something else, and the additional options show up in Create other; in our case, Name can also be a field. Choosing either of these options will generate the appropriate member inside the Person class.
After you execute the Create property action, you will see the following placeholder:

Figure 77
In this particular case, Rider has correctly guessed the type to be string. This inference only works if you assign something unambiguous; had we written person.Name = null;, we would be offered the type object instead.
The Create from Usage paradigm allows very rapid prototyping of your ideas: you write the code you want first, and generate the data structures to support that code on the fly.
Emmet (formerly known as Zen Coding) is a mini-DSL (Domain-Specific Language) for quickly creating complicated code structures in web languages (HTML and CSS). It is a form of code generation that uses a special interpreter to read the definition and expand it into a (possibly quite complicated) structure.
Rider comes with built-in support for Emmet, so that if you write, say, table>tr*3>td*2 and press Tab, Rider will turn it into:
Code Listing 6
<table> <tr> <td></td> <td></td> </tr> <tr> <td></td> <td></td> </tr> <tr> <td></td> <td></td> </tr> </table> |
It’s worth noting that, upon the generation of the HTML shown in Code Listing 5, Rider will also create hotspots—places where you can enter the contents of the <td> elements—and will allow you to move from one to another using the Tab key.
Consult Emmet documentation for more info on which commands this DSL supports.
Mnemonic templates are my own invention. Essentially, the goal is to provide Emmet-like capabilities for common programming languages such as C#, Kotlin, and others. The mnemonic templates project is open-source, and is written in the F# programming language.
You can think of mnemonic templates as “Emmet for OOP languages,” since it pursues similar goals: to allow users to quickly generate classes, methods, properties, and other constructs through shorthand notation. While the DSL doesn’t directly support nesting, you can create common code structures with ease. Here are some of the templates and the constructs they create:
The set of types supported in Mnemonics is as follows:
Table 1
Abbreviation | Data Type |
|---|---|
b | bool |
c | char |
f | float |
by | byte |
d | double |
i | int |
m | decimal |
s | string |
l | long |
u | uint |
g | Guid |
t | DateTime |
sb | StringBuilder |
In addition to C# and VB.NET, the Mnemonics project also supports other programming languages and IDEs. Check out its readme page for more information about the different templates available. It’s also worth noting that Mnemonics, being entirely a code generation-based project (it generates a Cartesian product of templates and types listed in Table 1), is a good case study in the use of the F# programming language.