Page 21 - MSDN Magazine, November 2017
P. 21

Figure 1 Generating a New Type While in the Active Editor
The first option, Generate class ‘Person’ in new file, will generate a new class called Person with the internal modifier inside a new file called Person.cs. The second option, Generate class ‘Person,’ will generate a new class called Person with the internal modifier inside the current file. The third option, Generate nested class ‘Person,’ will generate a new private class called Person nested inside the type that’s currently active in the code editor (in this case the new class would be generated inside the Program class). You can then change the internal or private modifier either manually or by right-clicking the modifier and then still selecting Quick Fix. In all cases, the new class is generated with an empty constructor. The
fourth option, Generate new type, instead shows a dialog where you can specify what type you wish to create (class, interface or enumer- ation), the access modifier and the destination. And you’re not limited to this—you can use the same technique to add members to the newly generated type. You can simply assign or invoke a property, field or method that doesn’t yet exist and leverage quick fixes to add one easily, as demonstrated in Figure 2, which shows how to add a new property called LastName to the Person class. The Generate variable ‘LastName’ option pro- vides different suggestions based on the current context.
In this case, the code is making an assignment, so the code editor suggests you generate a new prop- erty or field. Based on the code, the
code editor will also assign the proper type to the newly generated member, which means it will generate a property (or field) of type string. The code editor can analyze method invocations and generate matching method signatures. This feature is part of the code refactoring tooling and helps you keep your focus on the code while writing.
Removing Redundant Code
The code editor in Visual Studio for Mac also highlights redundant code—code that’s not necessary and not utilized. Redundant code is easily recognizable because it’s grayed out. Behind the scenes,
redundant code is highlighted based on some source analysis rules that cause the redundant code to be considered an issue. (You can control these rules, but that’s out of scope here.) Most of the time you’ll find examples of redundant code based on unnecessary using directives, but redundant code recognition isn’t limited to this. For example, an empty constructor and the internal modifier are highlighted as redundant in Figure 3.
If you right-click some redundant code, which you recognize by its lighter color, you can then select Quick Fix and Visual Studio will show a code fix that will remove the unnecessary code. Additionally, you’ll see a submenu called Options that allows you to:
• Suppress the current issue detection.
• Configure the analysis rule further in the
Visual Studio preferences.
• Apply the code fix for multiple issues of
the same type at the document, project or solution level.
November 2017 17
Figure 2 Generating a New Member msdnmagazine.com



















































































   19   20   21   22   23