官术网_书友最值得收藏!

  • 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?

Tip

This is a standard approach when you write applications using Test Driven Design (TDD).

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:

Generating code for non-existent objects

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.

Tip

There are many problems that can cause an underlining of the code by ReSharper. To check what exactly is wrong, simply move your mouse cursor above the underlined word and ReSharper will display a pop up with a description of the problem that has occurred.

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:

Generating code for non-existent objects

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; }
 

Tip

Properties can be easily created using the prop snippet. Just write prop and double press Tab.

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:

Generating constructors

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)

Note

Surround with contains many more options. Check them and use them for your daily work.

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:

Generating object members

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.
主站蜘蛛池模板: 宜兰县| 乐至县| 武清区| 永和县| 贵州省| 白银市| 西青区| 调兵山市| 巴东县| 商丘市| 吉安县| 太康县| 京山县| 枞阳县| 乐东| 定边县| 正蓝旗| 平泉县| 东乌珠穆沁旗| 尤溪县| 隆尧县| 渝中区| 中牟县| 新蔡县| 怀柔区| 秦安县| 四会市| 昌黎县| 广元市| 德化县| 井研县| 罗定市| 金华市| 灵山县| 拜城县| 公主岭市| 石渠县| 汉阴县| 太仆寺旗| 石首市| 钟山县|