If you are using StyleCop in your projects, most of the time, you need to remove unused namespaces and sort them. In order to achieve best possible coding performance, you need to know as many hotkeys as possible.
Sort and Remove Usings
If we have the following using
s:
using System;
using System.IO;
using Microsoft.TeamFoundation.TestManagement.Client;
using Microsoft.TeamFoundation.Client;
using System.Collections.Generic;
using System.Dynamic;
If you run StyleCop with default settings, the following warnings will be displayed.
You can sort and remove unused using
s from the context menu.
Also, you can assign a new hotkey for the command above. To do so, open Options > Environment > Keyboard
Search for Edit.RemoveAndSort
. For Use new shortcut: choose Text Editor and assign the desired shortcut. Next time, you just need to press “Ctrl+Shift+W
” to remove unused namespaces and sort them accordingly.
Place System Directives First
The above strategies are not working in all cases for the warning: “Warning 15 SA1208: CSharp.Ordering: System using directives must be placed before all other using directives.” For example, if you have using Automapper; it will be always sorted on top of the System usings. You can control this behavior from the Visual Studio Options.
Options > Text Editor > C# > Advanced > Place ‘System’ directives first when sorting usings
You can find more detailed information in the official Microsoft documentation here.
So Far in the C# Series
1. Implement Copy Paste C# Code
2. MSBuild TCP IP Logger C# Code
3. Windows Registry Read Write C# Code
4. Change .config File at Runtime C# Code
5. Generic Properties Validator C# Code
6. Reduced AutoMapper- Auto-Map Objects 180% Faster
7. 7 New Cool Features in C# 6.0
8. Types Of Code Coverage- Examples In C#
9. MSTest Rerun Failed Tests Through MSTest.exe Wrapper Application
10. Hints For Arranging Usings in Visual Studio Efficiently
11. 19 Must-Know Visual Studio Keyboard Shortcuts – Part 1
12. 19 Must-Know Visual Studio Keyboard Shortcuts – Part 2
13. Specify Assembly References Based On Build Configuration in Visual Studio
14. Top 15 Underutilized Features of .NET
15. Top 15 Underutilized Features of .NET Part 2
16. Neat Tricks for Effortlessly Format Currency in C#
17. Assert DateTime the Right Way MSTest NUnit C# Code
18. Which Works Faster- Null Coalescing Operator or GetValueOrDefault or Conditional Operator
19. Specification-based Test Design Techniques for Enhancing Unit Tests
20. Get Property Names Using Lambda Expressions in C#
21. Top 9 Windows Event Log Tips Using C#
If you enjoy my publications, feel free to SUBSCRIBE
Also, hit these share buttons. Thank you!
The post- Hints For Arranging Usings in Visual Studio Efficiently appeared first on Automate The Planet.