- ReSharper Essentials
- ?ukasz G?sior
- 1025字
- 2021-07-19 18:20:04
Code generation
There are many other ways to write code rather than simply pressing keys on your keyboard. ReSharper comes with many features that can generate code for you. You can find some of these features directly in Visual Studio but ReSharper comes with more. Even if ReSharper comes with some feature that exists in Visual Studio, ReSharper provides more user-friendly ways to use it.
Generating code for non-existent objects
Usually, when you are designing a class in your project, you start by writing complete code, such as class name, properties, and functions. When you are done, you can use that class in your application.
What if I told you there is a different way?
How about you start using your class before creating it and then let ReSharper create what you need?
As the best way to learn something is to start doing it, we will show you how ReSharper helps with generating code.
Let's open Visual Studio and create a new console application project.
Note
To create a new console application project, navigate to FILE | New Project… from the Visual Studio toolbar. This will open a New Project window. Navigate to Installed | Templates | Visual C# | Windows from the left-hand menu and click on Console Application from the list of available projects.
By default, the console application project comes with one Program.cs
file. Let's open it and write the following line inside the Main(string[] args)
method:
new UserManager();
Your code should look like the following screenshot:

The preceding screenshot is a great example to show you the power of ReSharper. The name of our class, UserManager
, is marked in red, which means that this class does not exist. This is also marked by a red line on the marker bar. The new
keyword is underlined in blue to tell you that you are creating a new object without assigning it to any variable. The args
parameter in the Main
method and all using
statements are grayed out to show you they are not used.
Finally, there is a context action icon, which tells you that there are some ReSharper actions that you can run on this code.
Before we continue, please have a look at the same code in the following screenshot, but without ReSharper:

Without ReSharper, it looks like everything is fine with this code, doesn't it?
As you remember from the previous chapter, if there is a context action icon, you should press Alt + Enter. Then, select Create class 'UserManager' from the displayed options. This will create a new internal class, UserManager
. To change this, move your cursor to the following line:
internal class UserManager
Press Alt + Enter and select To public. Now your new class is public.
In the same way, you can create methods, properties, enums, and everything you need. Just write the necessary code and press Alt + Enter!
Introduce variable
Now, let's come back to our first line. The new
keyword is still underlined so we need to fix this. Move your cursor to the following line and press Ctrl + R, V (this means that you press Ctrl and R at the same time and then release R while still holding Ctrl, and then press V):
new UserManager();
This shortcut is associated with the Introduce variable feature and will convert your code to the following line:
var userManager = new UserManager();
Generating constructors
Now let's add a constructor to our class. In this constructor, we will assign a new value to the property Repository
.
We do not have such a property yet, so let's create it inside the class. You can write the following code:
public UserRepository Repository { get; set; }
Now we are ready to create our constructor. Press Alt + Insert and from the newly displayed menu, select Constructor. This will open a new dialog window in which you can configure your constructor—you can select a constructor from a base class that you would like to implement. You can select the properties and variables that you would like to set in this constructor and change the access rights. A sample view of this screen is presented in the following screenshot:

In our case, just check Repository:UserRepository and click on Finish.
As we created our constructor, ReSharper marked our first line because we are using their default constructor without parameters. By pressing Alt + Enter, you can generate a second constructor without parameters.
In your code, the UserRepository
class is still marked in red—you already know what to do with this!
Surround with
Now, let's assume that we would like to assign a new value to our property only if the parameter in the constructor is not null. Move your cursor to the following line:
Repository = repository;
Press Ctrl + E, U and select if
from the list. Now write your condition as shown in the following code:
if (repository != null)
Generating object members
So far, we have used Alt + Insert only to create the constructor but as you can see in the following screenshot, there are more interesting options:

Let's describe those that are used the most:
- Read-only properties and Properties: These allow you to create properties for variables.
- Missing members: This is very useful when your class implements an interface or extends a class. This option allows you to create all members (methods, properties) that need to be implemented in your class.
- Overriding members: This allows you to override methods from inherited members. For example, you can use this option to override the
ToString()
method.
- Clojure Programming Cookbook
- Fundamentals of Linux
- WebAssembly實戰
- Learning ASP.NET Core 2.0
- Learning Informatica PowerCenter 10.x(Second Edition)
- Python數據分析(第2版)
- Drupal 8 Configuration Management
- Unity 5 for Android Essentials
- Learning Laravel's Eloquent
- Python語言實用教程
- Java SE實踐教程
- 搞定J2EE:Struts+Spring+Hibernate整合詳解與典型案例
- Visual Basic 6.0程序設計實驗教程
- PHP+MySQL動態網站開發從入門到精通(視頻教學版)
- Java EE架構設計與開發實踐