Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#

Hints For Arranging Usings in Visual Studio Efficiently

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
21 Feb 2015CPOL2 min read 8K   12  
This post gives some hints for arranging usings in Visual Studio efficiently.

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 usings:

C#
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.

Image 1

You can sort and remove unused usings from the context menu.

Image 2

Also, you can assign a new hotkey for the command above. To do so, open Options > Environment > Keyboard

Image 3

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

Image 4

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Automate The Planet
Bulgaria Bulgaria
CTO and Co-founder of Automate The Planet Ltd, inventor of BELLATRIX Test Automation Framework, author of "Design Patterns for High-Quality Automated Tests: High-Quality Test Attributes and Best Practices" in C# and Java. Nowadays, he leads a team of passionate engineers helping companies succeed with their test automation. Additionally, he consults companies and leads automated testing trainings, writes books, and gives conference talks. You can find him on LinkedIn every day.

Comments and Discussions

 
-- There are no messages in this forum --